import java.io.BufferedReader; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; import java.util.*; public class first{ public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new FileReader("D:\test.txt"));//新建缓存区读取为所需统计文件的读取 StringBuffer mp= new StringBuffer(); String s; ; while (( s=br.readLine())!= null) { mp.append(s); } Map<String,Integer> map = new HashMap<String, Integer>();//运用哈希排序的方法进行排序 StringTokenizer st = new StringTokenizer(mp.toString(),",.! ");//分割字符串 //用来测试是否有此标记生成器的字符串可以有更多的标记。并把分割好的单词保存在letter字符串中。 while (st.hasMoreTokens()) { String letter = st.nextToken(); int count; if (map.get(letter) == null) { count = 1;//表明了没有进行分割。 } else { count = map.get(letter).intValue() + 1; } map.put(letter,count); } Set<WordEntity> set = new TreeSet<WordEntity>(); for (String key : map.keySet()) { set.add(new WordEntity(key,map.get(key))); } int count = 1; for (Iterator<WordEntity> it = set.iterator(); it.hasNext(); ) { WordEntity w = it.next(); if (count == 10)// 当输出10个后跳出循环 break; count++; } } catch (FileNotFoundException e) { System.out.println("文件未找到~!");//异常处理 } catch (IOException e) { System.out.println("文件读异常~!");//异常处理 } } public void countWord(String string) { // TODO Auto-generated method stub } } class WordEntity implements Comparable<WordEntity> { private String key; private Integer count; public WordEntity (String key, Integer count) { this.key = key; this.count = count; } public int compareTo(WordEntity o) { int cmp = count.intValue() - o.count.intValue(); return (cmp == 0 ? key.compareTo(o.key) : -cmp); //只需在这儿加一个负号就可以决定是升序还是降序排列 -cmp降序排列,cmp升序排列 //因为TreeSet会调用WorkForMap的compareTo方法来决定自己的排序 } public String toString() { return key + " 出现的次数为:" + count; } public String getKey() { return key; } public Integer getCount() { return count; } } import static org.junit.Assert.*; import org.junit.Test; public class TestTest { @Test public void testCountWord() { first demo = new first(); demo.countWord("Hello World My First Unit Test"); } }