日期:2014-05-20 浏览次数:21049 次
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()); } } }
/**
*
*/
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());
}
}
/**
*
*/
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);
}*/
}