package com.heima.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeMap;
public class Test2 {
/**
* * 获取一个文本上每个字符出现的次数,将结果写在times.txt上
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));
TreeMap<Character, Integer> tp = new TreeMap<>();
int key;
while((key = br.read()) != -1) {
char c = (char)key;
tp.put(c, !tp.containsKey(c) ? 1 : tp.get(c) + 1);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("time.txt"));
for (Character key1 : tp.keySet()) {
switch (key1) {
case '\t':
bw.write("\\t" + "=" + tp.get(key1));
break;
case '\r':
bw.write("\\r" + "=" + tp.get(key1));
break;
case '\n':
bw.write("\\n" + "=" + tp.get(key1));
break;
default:
bw.write(key1 + "=" + tp.get(key1));
break;
}
bw.newLine();
}
bw.close();
}
}
|
|