簡體   English   中英

Java程序中線程的字數統計

[英]Word counting for thread in java program

我的任務是編寫一個程序,通過創建進程或線程來查找目錄中所有文件(也包括嵌套目錄)的字數計數。我需要為每個文件以及該進程或線程創建進程或線程將計算文件中的單詞數。 當進程或線程完成計數時,它將寫入文件名以及該文件中有多少個單詞。 最后,將顯示所有文件的單詞總數。 對於我的任務,我編寫了以下程序:

package cloudS;

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException;
import java.io.File; 

public class WordCount1 
{
    private static int count = 0; 

    public static void main (String [] args)
    {
        String path = "C:\\Users\\$mit\\Desktop";
        path = path + "\\" + args[0];
        String filename = args[0];  
        task1(filename, path);
        task2(filename, path);
        System.out.println("Total Word " + count);
    }

    public static void task2 (String filename, String path)
    {
        File file = new File (path);
        if (file.isDirectory())
        {
            String [] name = file.list();
            String [] paths = new String [name.length];  

            for (int i=0; i<name.length; i++)
            {
                paths[i] = path + "\\" + name[i];
                task2(name[i], paths[i]);
            }
        }
        else
        {
            BufferedReader br = null; 
            FileReader fr = null; 

            int wordCount = 0; 
            try 
            {
                fr = new FileReader(path);
                br = new BufferedReader (fr);

                String line = null; 

                while ((line = br.readLine()) != null)
                {
                    String [] array = line.split(" ");
                    wordCount = wordCount + array.length;
                    count = count + array.length;
                }
            }

            catch (IOException e)
            {
                System.out.println("IOException");
            }

            System.out.println (filename + "\t" + wordCount);

            try 
            {
                br.close(); 
                fr.close(); 
            }
            catch (IOException e)
            {
                System.out.println("IOException");
            }
        }
    }

    public static void task1 (String filename, String path)
    {
            BufferedReader br = null; 
            FileReader fr = null; 

            int wordCount = 0; 
            try 
            {
                fr = new FileReader(path);
                br = new BufferedReader (fr);

                String line = null; 

                while ((line = br.readLine()) != null)
                {
                    String [] array = line.split(" ");
                    wordCount = wordCount + array.length;
                }
            } 
            catch (IOException e)
            {
                System.out.println("IOException");
            }

            System.out.println (filename + "\t" + wordCount);

            try 
            {
                br.close(); 
                fr.close(); 
            }

            catch (IOException e)
            {
                System.out.println("IOException");
            }
    }


}

運行該程序后,出現以下錯誤:線程“ main”中的異常java.lang.ArrayIndexOutOfBoundsException:cloudS.WordCount1.main(WordCount1.java:15)為0

任何幫助表示贊賞

您是否向程序傳遞了任何參數? 看起來您的args數組可能為空,但是您正在嘗試使用args[0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM