note:没有词频排序功能。该程序用于实现统计词频功能,从文件读取内容,将统计结果输出到文件。
import java.util.*;
import java.io.*;
public class wordRate {
public static void main(String[] args) throws Exception {
BufferedReader infile = new BufferedReader(new FileReader("article.txt"));
String string;
String file = null;
while ((string = infile.readLine()) != null) {
file += string;
}
file = file.toLowerCase();// 所有字母小写化
file = file.replaceAll("[^A-Za-z]", " ");// 正则,匹配非英文字符为空格
file = file.replaceAll("\s+", " "); // 正则,将1到多个空格匹配为一个空格
String words[];
words = file.split("\s+");// 取出单词,并将单词存入数组中
Map hashMap = new HashMap();
for (int i = 0; i < words.length; i++) {
String key = words[i];
if (hashMap.get(key) != null) {
int value = ((Integer) hashMap.get(key)).intValue();
value++;
hashMap.put(key, new Integer(value));
} else {
hashMap.put(key, new Integer(1));
}
}
Map treeMap = new TreeMap(hashMap);
Set entrySet = treeMap.entrySet();
// ================================modify==========================
BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt")); //实例化一个writer对象
for (Iterator iterator = entrySet.iterator(); iterator.hasNext();) { // 循环迭代
//注意:以下一行不能写成String result =String result =(String)iterator.next();
String result =iterator.next().toString();
bw.write(result); // 调用writer的write方法将得到的result写入文件
bw.newLine(); // 另起一行
bw.flush(); // 清空缓存
}
}
}