0

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);
                }
            }
        }
    }
}

1 Answer 1

1

You only set one output-file "out.txt". You'd have to use multiple files.

For example:

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);
        }
        //Here use Printwriter
        try(PrintWriter pw = new PrintWriter(file.getName() + "_out.txt") {
            //iterate over entryset and clear the map after with map.clear()
        }
    }   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.