public static void demo6() throws FileNotFoundException, IOException {
BufferedReader br =
new BufferedReader(new FileReader("xxx.txt"));
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
int ch;
while((ch = br.read()) != -1){
char c = (char) ch;
tm.put(c, !tm.containsValue(c)?1:tm.get(c)+1);
}
//System.out.println(br);
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt"));
for (Character st : tm.keySet()) {
//bw.write(st);
//bw.newLine();
// System.out.println(st);
switch (st) {
case '\t':
bw.write("\\t"+"="+tm.get(st));
break;
case '\n':
bw.write("\\n"+"="+tm.get(st));
break;
case '\r':
bw.write("\\r"+"="+tm.get(st));
break;
default:
bw.write(st+"="+tm.get(st));
break;
}
bw.newLine();
}
bw.close();
} |
|