フォルダ内のすべてのテキストファイルを読み込み、各ファイルの特定の単語を数え、単語の頻度をtxtファイルに書きたい。ここに私のコードです:Java:特定の単語の頻度をカウントしてファイルに書き込む
public static void main(String[] args) throws IOException{
String[] array = new String[]{"beautiful", "good", "extraordinary", "wonderful", "like",
"proud","brilliant","great","well", "perfect"};
int[] wordCount = new int[]{0,0,0,0,0,0,0,0,0,0};
File path = new File("development/text");
for(File file: path.listFiles()){
PrintWriter writer = new PrintWriter(new FileWriter("result.txt",true));
FileInputStream fin = new FileInputStream(file);
Scanner s = new Scanner(fin);
for(int i = 0; i < array.length; i++)
{
wordCounter = 0;
while (s.hasNext())
{
if (s.next().equals(array[i])) {
wordCount[i]++;
}
}
}
writer.println(wordCount[0] + "," + wordCount[1] + "," + wordCount[2] + "," +
wordCount[3] + "," + wordCount[4] + "," +wordCount[5] + "," +wordCount[6]
+ "," +wordCount[7] + "," + wordCount[8] + "," + wordCount[9]);
fin.close();
s.close();
writer.close();
}
}
しかし、私のコードは、配列(美しい)の最初の要素を読み取るだけです。他の要素の出力はテキストファイルにもかかわらず0です。