在java程序中计算线程的字

时间:2017-09-11 02:52:34

标签: java multithreading thread-safety

我的任务是编写一个程序来查找目录中所有文件的字数(也用于嵌套 目录)通过创建进程或线程。我需要为每个文件创建进程或线程,并且 此进程或线程将计算文件内的单词数。当进程或线程 完成计数,它将写入文件名和该文件中有多少个单词。在 结束,将显示所有文件的总字数。 为了我的任务,我做了以下计划:

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:0     在cloudS.WordCount1.main(WordCount1.java:15)

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

您是否将任何参数传递给该计划?看起来您的args数组可能是空的,但您尝试使用args[0]