私はJavaプログラミングに慣れていないので、名前を出力しようとしていて、フォルダーから複数のテキストファイルを読み込んで、各単語ファイルの単語頻度を数えています。フォルダーを読んでいるときに、すべてのテキストファイルが印刷されますが、読み取られません。コードを見てください。
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);
}
}
}
}
}
出力:
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)
{}