8、编程实现统计文本文件中某个单词的出现频率,并输出统计结果

package _0831;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
public class CountNum {
 public static void countNum() throws IOException {
  BufferedReader br = null;
  try {
   br = new BufferedReader(new FileReader("c://file.txt"));
   Map map = new HashMap();
   for (String s = br.readLine(); s != null; s = br.readLine()) {
    StringTokenizer st = new StringTokenizer(s, ",.;");
    while (st.hasMoreTokens()) {
     String temp = st.nextToken();
     if (map.containsKey(temp)) {
      map.put(temp, new Integer((Integer) map.get(temp) + 1));
     } else {
      map.put(temp, new Integer(1));
     }
    }
   }
   for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
    Map.Entry entry = (Map.Entry) it.next();
    System.out.println(entry.getKey() + "-->" + entry.getValue()+ "times");
   }
  } finally {
   br.close();
  }
 }
}

 

你可能感兴趣的:(java,编程,C++,c,C#)