-4

I am new to Java programming, I am trying to print names, read multiple text files from a folder and counting the word frequency of each word file, when i am reading a folder all the text files are printed but they are not read, please look at the code.

import java.io.*;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class foldersearch
{
    public static void main(String[] args) 
    {
        // Directory path here
        String path = "/home/sumeet/Documents/text files"; 

        String files;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles(); 

        for (int i = 0; i < listOfFiles.length; i++) 
        {
            if (listOfFiles[i].isFile()) 
            {
                files = listOfFiles[i].getName();
                if (files.endsWith(".txt") || files.endsWith(".TXT"))
                {
                    System.out.println(files);
                    TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>(); 

                    String currentLine="";

                    File textFile = new File(files); // SOME CHANGE IS REQUIRED HERE..?
                    try {
                        BufferedReader br = new BufferedReader(new FileReader(textFile)); 

                        while ((currentLine = br.readLine()) != null) { 
                            currentLine = currentLine.toLowerCase(); 
                            StringTokenizer parser = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'"); 
                            while (parser.hasMoreTokens()) { 
                                String currentWord = parser.nextToken(); 
                                Integer frequency = frequencyMap.get(currentWord); 
                                if (frequency == null) { 
                                    frequency = 0; 
                                } 
                                frequencyMap.put(currentWord, frequency + 1); 
                            } 
                        }
                        br.close(); 
                    } catch (FileNotFoundException e) {
                        System.out.println(e.getMessage());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(frequencyMap);
                }
            }
        }
    }
}

Output:

nokia.txt
nokia.txt (No such file or directory)
{}
MainClass.txt
MainClass.txt (No such file or directory)
{}
2b.txt
2b.txt (No such file or directory)
{}
cn exercise 2.txt
cn exercise 2.txt (No such file or directory)
{}
2c.txt
2c.txt (No such file or directory)
{}
dummy.txt
dummy.txt (No such file or directory)
{}
readme.txt
readme.txt (No such file or directory)
{}
Kb.txt
Kb.txt (No such file or directory)
{}
all.txt
all.txt (No such file or directory)
{}
1b.txt
1b.txt (No such file or directory)
{}
todo.txt
todo.txt (No such file or directory)
{}
1c.txt
1c.txt (No such file or directory)
{}
2a.txt
2a.txt (No such file or directory)
{}
USE CASE.txt
USE CASE.txt (No such file or directory)
{}
2
  • 1
    Also, please learn how to format your code so it's readable - i.e. where to put line breaks and how to indent your code. One guide is here: oracle.com/technetwork/java/codeconv-138413.html
    – user9876
    Dec 16, 2011 at 14:23
  • 1
    Why is this 'Vote to close - not a real question'? Besides formatting issues, this is a reasonable and correct question.
    – KevinDTimm
    Dec 16, 2011 at 14:28

3 Answers 3

1

You need to prepend the name of the directory to the filename before you attempt to open it. It's currently trying to open nokia.txt/2b.txt/etc. from the current directory.

5
  • can u show me how, i am new to java.?
    – sum2000
    Dec 16, 2011 at 14:32
  • Simple String manipulation - files = path + File.separator + listOfFiles[i].getName();
    – KevinDTimm
    Dec 16, 2011 at 14:33
  • Java's File API, while not perfect, has methods that will do this for you. But in this case, it's not necessary if you follow sje397's suggestion. Dec 16, 2011 at 14:41
  • OUTPUT: 'nokia.txt /home/sumeet/Documents/text files/nokia.txt (No such file or directory) {}'
    – sum2000
    Dec 16, 2011 at 14:42
  • just read the code once before u comment , i m printing name first and reading afterwards..seems like u are mistaken somewhere, i did what u told..and ya i m total newbie to java...
    – sum2000
    Dec 16, 2011 at 15:54
1

Try:

File textFile = new File(folder.getAbsolutePath() + File.separator + files);
0
1

One of the first things a programmer needs to learn is how to figure out for oneself what is happening when one gets an error. So we wouldn't be helping you much by just giving the answer (though I see that we already have).

Another core skill all programmers need to acquire is breaking up complexity into manageable chunks. Even for someone who's been programming in Java for years, it's an effort to figure out what's going on in the code you posted.

A further skill that's very important as you progress further is knowing the standard library and using it when you can. This can often save you from reinventing the wheel.

In light of the above 3 points, here's my advice:

  • Split your code into one method that takes a File (or a String representing a filename), and counts word frequency in that file. And another method that finds and loops through all the files you want to read, calling the first method on each.
  • Test your two methods separately, which will help you track down the bug.
  • Look into the FileFilter interface.
  • Carefully read the Javadoc of the File.name() method.
1
  • thanks for your advice..will keep in mind....
    – sum2000
    Dec 16, 2011 at 14:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.