繁体   English   中英

读取和处理多个文本文件

[英]Reading and processing multiple text files

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

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

文本文件2:天气好的天气

输出会显示

你好2

我的2

名字1

是3

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

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

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

约翰1

你好2

我的2

很好1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM