日期:2014-05-20  浏览次数:20878 次

100分求讨论多线程文本文件单词统计
现在有一个文本文件叫做index.txt,里面有很多行,每一行都是一个文件名,每个文件名所指向的文件里都包含一些英文的段落(以空格隔开的单词)。现在想在读每一行的时候创建一个进程,该进程统计每个英文单词出现的次数并且最后汇总所有单词的出现次数并字母顺序打印如
apple 3
beach 1
me 5
zero 5

本人已经完成了单词统计的部分(尚未完成重复词计数),突然觉得自己的想法可能满足不了实现这个程序的要求,所以散100分和高手们讨论求点思路。如果能有高手给几段代码是最好,但是也十分欢迎一起讨论你的想法。完成部分如下:
ZhengProject4.java
Java code
import edu.truman.cs260.Zheng.RunIndex;
import edu.truman.cs260.Zheng.Counter;
import edu.truman.cs260.Zheng.Word;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//import java.util.ArrayList;
import java.util.ArrayList;

/**
 * @author Tian
 *
 */
public class ZhengProject4 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Runnable counter = new RunIndex(null);
        //ArrayList<String> names = new ArrayList<String>();
        File file = new File("index.txt");
        BufferedReader reader = null;
        
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null) 
            {
                //System.out.println(text);
                counter = new RunIndex(text);
                Thread t1 = new Thread(counter);
                t1.start();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //ArrayList <Counter> counters;
            //Counter counters = new Counter(null);
            //for (Counter counter : counters) System.out.println (counters.getCounter()+ ": "+counters.getEvent());
        }
    }
}



RunIndex.java
Java code
/**
 * 
 */
package edu.truman.cs260.Zheng;

import java.lang.Runnable;
import java.io.File;
import java.util.ArrayList;

/**
 * @author Tian
 *
 */
public class RunIndex implements Runnable{
    
    
    private String indexName;

    public RunIndex(String aIndexName) {
        indexName = aIndexName;
    }
    
    public void run() {
         TextReader parser=new TextReader(new File(indexName)); 
         ArrayList <Word> words = parser.readIn(); 
         for (Word word : words) System.out.println (word.getWord()+ ": "+word.getEvent()); 
    }

}



TextReader.java:
Java code
/**
 * 
 */
package edu.truman.cs260.Zheng;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; 
import java.util.StringTokenizer; 

/**
 * @author Tian
 *
 */
public class TextReader {
    
    private File file;

    public TextReader (File aFile) {
        assert aFile.exists() && aFile.isFile();
        file = aFile;
    }
    
     public ArrayList <Word> readIn(){ 
         BufferedReader input = null; 
         try { 
             ArrayList <Word> words = new ArrayList <Word> (); 
             input = new BufferedReader(new FileReader(file)); 
             String line; 
             while ((line = input.readLine()) != null) { 
                 StringTokenizer tokenizer=new StringTokenizer(line); 
                 while (tokenizer.hasMoreTokens()) { 
                     String token=tokenizer.nextToken(); 
                     Word word=new Word(token); 
                     int index=words.indexOf(word); 
                     if (index==-1) { 
                         word.increase(); 
                         words.add(word); 
                     } else words.get(index).increase(); 
                  } 
              } 
              input.close(); 
              Collections.sort(words); 
              return   words; 
         } catch (Exception e) { 
             e.printStackTrace(); 
             return null; 
         } finally { 
             if (input!= null) { 
                 try { 
                     input.close(); 
                 } catch(IOException e) { 
                   } 
             } 
         } 
 } 
    
    
    
    
    
    
    
    
/*    public void readIn () {

        //StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;
        ArrayList<String> names = new ArrayList<String>();
        
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            String tester = null;
            
            //Iterator<String> it = names.iterator();
            while ((tester = reader.readLine()) != null) 
            {
                System.out.println(tester);
                names.add(text);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(names);
    }*/
}