I am trying to generate word frequency for each of the files in a directory and get the output into multiple files . The input files contain sentences and the output files contain words and the number of times they are repeated.
With the following code I was able to read from multiple files at once but the output is generated into one single file .
I want to generate the output of each input file into different files.
Code:
import java.util.*;
import java.io.*;
public class words {
public static void main(String args[]) throws Exception {
File dir = new File("foldername");
// Create a TreeMap to hold words as key and count as value
Map<String, Integer> map = new TreeMap<>();
for(File file : dir.listFiles())
{
try ( // Create an input stream
Scanner input = new Scanner(file);
) {
while (input.hasNext()) {
String[] words = input.nextLine().split("[ \n\t\r\"\'.,;:!?()]");
store(map, words);
}
}
// Get all entries into a set
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
//To write the output into a text file
PrintWriter out = new PrintWriter("out.txt");
// Get key and value from each entry
for (Map.Entry<String, Integer> entry: entrySet)
out.println(entry.getValue() + "\t" + entry.getKey());
out.close();
}
}
/*To sort the occurrence of words*/
private static void store(Map<String, Integer> map, String[] words) {
for (int i = 0; i < words.length; i++) {
String key = words[i].toLowerCase();
if (key.length() > 0 && Character.isLetter(key.charAt(0))) {
if (!map.containsKey(key)) {
map.put(key, 1);
}
else {
int value = map.get(key);
value++;
map.put(key, value);
}
}
}
}
}