簡體   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