解决格式,还有当有标点符号时统计出现错误.
github:https://github.com/dailinlernhard/wordnum/blob/master/Compute.java
package wordnum;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class Compute {
public static void main(String[] args) throws IOException {
System.out.println("输入文件名及路径");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String file=null;
file=br.readLine();
System.out.println(file);
word(file);
}
/*
* 对单词进行个数统计,hashtable
*/
public static void word(String file) throws IOException{
BufferedReader br=new BufferedReader( new FileReader(file));
String line=null;
Hashtable ht=new Hashtable();
/*
* 读取文件,并切分,把单词存入hashtable
*/
while((line=br.readLine())!=null){
String []splitword=null;
splitword=line.split("[\\.\\s\\?\\‘\\,]");
for(String st:splitword){
String key=st;
if(ht.containsKey( key)){
int num=((Integer)ht.get(key)).intValue()+1;
ht.put(key,new Integer(num));
}
else ht.put(key, new Integer(1));
}
BufferedWriter bw=new BufferedWriter(new FileWriter("D:/Desktop.txt"));
/*
* 循环写入
*/
for(Iterator it=ht.keySet().iterator();it.hasNext();){
String key= (String) it.next();
Object value = ht.get(key);
System.out.println(key+":"+value);
String a=key;
a+=":";
a+=value.toString();
bw.write(a);
bw.flush();
bw.newLine();
}
}
}
}