读取和处理多个文本文件

时间:2017-04-29 10:48:30

标签: java multithreading file

我尝试创建一个程序,该程序将读取某个目录中的多个文本文件,然后生成所有文本文件中出现的单词的频率。

实施例

  

文本文件1:你好,我的名字是约翰你好我的

     

文本文件2:天气好天气

输出显示

  

你好2

     

我的2

     

名称1

     

是3

     

john 1

     

2

     

天气2

     

很好1

我遇到的问题是我的程序一旦运行就会终止,根本不显示任何输出。

这是我的班级

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

这是我的班级来运行它们

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

1 个答案:

答案 0 :(得分:2)

更新回答。问题的原因我错了。问题更多的是算法问题,而不是与线程相关的问题。

Mainstackquestion类的代码:

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

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

使用您提供的相同2个文件进行测试。

获得的结果:

  

2

     

名称1

     

天气2

     

是3

     

john 1

     

你好2

     

我的2

     

很好1