问题
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)
{}
回答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.
回答2:
Try:
File textFile = new File(folder.getAbsolutePath() + File.separator + files);
回答3:
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.
来源:https://stackoverflow.com/questions/8535469/reading-data-from-multiple-text-files