java - 读取和处理多个文本文件

标签 java multithreading file

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

例子

text file 1: hello my name is john hello my

text file 2: the weather is nice the weather is

输出会显示

hello 2

my 2

name 1

is 3

john 1

the 2

weather 2

nice 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 个文件进行测试。

得到的结果:

the 2

name 1

weather 2

is 3

john 1

hello 2

my 2

nice 1

关于java - 读取和处理多个文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694684/

相关文章:

php - 通过(相似)名称查找文件 PHP

java - 将 ArrayList 写入/读取文件的奇怪问题

java - Java中使用递归的不同方式

java - Java HashMap 中的线程问题

javascript - 在 Javascript 中同时做两件事

c# - 在 C# 中从已运行的任务中初始化新任务

java - 点击按钮后隐藏java中的框架

java: "downcasting"到新对象/切片相反

python - Python为什么要切换线程?

html - 角度输入文件 : Selecting the same file