- import java.io.*;
- import java.util.*;
- public class Test7 {
- private static Integer value;
- public static void main(String[] args) {
- FileReader fr = null;
- FileWriter fw = null;
- try {
- fr = new FileReader("e:\\a.txt");
- fw = new FileWriter("e:\\b.txt");
- TreeMap<Character,Integer> hm = new TreeMap<Character,Integer>();
- value = null;
- int count = 0;
- while((fr.read())!=-1)
- {//这里隔一个读一个,
- char ch = (char) fr.read();
- //System.out.println(ch);
- if(" ".equals(ch))
- continue;
- value = hm.get(ch);
- if(value != null)
- count = value;
- count++;
- hm.put(ch, count);
- count = 0;
- }
- System.out.println(hm.size());
- StringBuilder sb = new StringBuilder();
- Set<Map.Entry<Character, Integer>> entrySet = hm.entrySet();
- Iterator<Map.Entry<Character, Integer>>it = entrySet.iterator();
- while(it.hasNext())
- {
- Map.Entry<Character, Integer>me = it.next();
- Character chs = me.getKey();
- Integer counts = me.getValue();
- sb.append(chs+" "+counts+"次 ");
- }
- fw.write(sb.toString()+"\r\n");
- fw.flush();
- System.out.println(sb.toString()+"\r\n");
-
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- }
复制代码
|
|