Reading and processing multiple text files

I tried to create a program that will read multiple text files in a specific directory and then generate the frequency of the words that appear in all text files.

Example

text file 1: hello my name is john hello my

text file 2: weather good weather

The output will display

hi 2

my 2

name 1

is 3

john 1

2

weather 2

nice 1

The problem I'm running into is that my program just exits, as soon as it starts, no output is output at all.

Here's my class

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;

public class WordCountstackquestion implements Runnable {

    public String filename;

    public WordCountstackquestion(String filename) {
        this.filename = filename;
    }

    public void run() {
        File file = new File("C:\\Users\\User\\Desktop\\files\\1.txt");

        if (file.isDirectory()) {
            Scanner in = null;

            for (File files : file.listFiles()) {
                int count = 0;
                try {
                    HashMap<String, Integer> map = new HashMap<String, Integer>();
                    in = new Scanner(file);

                    while (in.hasNext()) {
                        String word = in.next();

                        if (map.containsKey(word)) {
                            map.put(word, map.get(word) + 1);
                        }
                        else {
                            map.put(word, 1);
                        }
                        count++;

                    }
                    System.out.println(file + " : " + count);

                    for (String word : map.keySet()) {
                        System.out.println(word + " " + map.get(word));
                    }
                } catch (FileNotFoundException e) {
                    System.out.println(file + " was not found.");
                }
            }
        }
    //in.close();
    }
}

      

Here's my class to run

public class Mainstackquestion {
    public static void main(String args[]) {
        if (args.length > 0) {
            for (String filename : args) {                          
                CheckFile(filename);
            }
        }
        else {
            CheckFile("C:\\Users\\User\\Desktop\\files\\1.txt"); 
        }
   }

    private static void CheckFile(String file) {
        Runnable tester = new WordCountstackquestion(file);
        Thread t = new Thread(tester);
        t.start();
    }
}

      

+3


source to share


1 answer


UPDATED . I was wrong about the original cause of the problem. The problem was more of an algorithm problem than a thread related problem.

Code for the Mainstackquestion

class:

public class Mainstackquestion {
       public static void main(String args[])
       {
           List<Thread> allThreads = new ArrayList<>();

           if(args.length > 0) {
               for (String filename : args) {
                   Thread t = CheckFile(filename);
                   allThreads.add(t);  // We save this thread for later retrieval
                   t.start(); // We start the thread
               }
           }
           else {
               Thread t = CheckFile("C:\\Users\\User\\Desktop\\files"); 
               allThreads.add(t);
               t.start();               
           }

           try {
               for (Thread t : allThreads) {
                   t.join(); // We wait for the completion of ALL threads
               }
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }

     private static Thread CheckFile(String file) {
         Runnable tester = new WordCountstackquestion(file);
         return new Thread(tester);
     }
}

      

Code for WordCountstackquestion

:



import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;

public class WordCountstackquestion implements Runnable {

    public String filename;

    public WordCountstackquestion(String filename) {
        this.filename = filename;
    }

    public void run() {
        File dir = new File(filename);

        if (dir.exists() && dir.isDirectory()) {
            Scanner in = null;

            HashMap<String, Integer> map = new HashMap<String, Integer>();

            for (File file : dir.listFiles()) {
                if (file.exists() && !file.isDirectory()) {
                    int count = 0;
                    try {
                        in = new Scanner(file);
                        while (in.hasNextLine()) {
                            String line = in.nextLine();
                            String[] words = line.split(" ");

                            for (String w : words) {
                                if (map.containsKey(w)) {
                                    map.put(w, map.get(w) + 1);
                                } else {
                                    map.put(w, 1);
                                }
                            }
                            count++;

                        }

                        //System.out.println(file + " : " + count);
                    } catch (FileNotFoundException e) {
                        System.out.println(file + " was not found.");
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                }
            }

            for (String word : map.keySet()) {
                System.out.println(word + " " + map.get(word));
            }
        }
    }
}

      

Tested with the same 2 files you listed as an example.

The result is:

2

name 1

weather 2

is 3

john 1

hi 2

my 2

nice 1

+2


source







All Articles