我只是入学教程视频的搬运工- public class Demo08 {
- /**
- * 获取一个文本上每个字符出现的次数,将结果写在times.txt上
- * 分析:
- * 1,创建带缓冲的输入流对象
- * 2,创建双列集合对象TreeMap
- * 3,将读到的字符存储在双列集合中,存储的时候要做判断,如果不包含这个键,就将键和1存储进去,如果包含,就将该值+1
- * 4,关闭输入流
- * 5,创建输出流对象
- * 6,遍历集合将集合中的内容写到times.txt
- * 7,关闭输出流
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
-
- BufferedReader br = new BufferedReader(new FileReader("aaa.txt"));
-
- TreeMap<Character,Integer> tm = new TreeMap<>();
-
- int ch;
- while ((ch = br.read()) != -1) {
- char c = (char)ch;
- tm.put(c, !tm.containsKey(c) ? 1 : tm.get(c) + 1);
- }
- br.close();
-
- BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));
-
- for(Character key : tm.keySet()) {
- switch (key) {
- case '\t':
- bw.write("\\t" + "=" + tm.get(key));
- break;
- case '\n':
- bw.write("\\n" + "=" + tm.get(key));
- break;
- case '\r':
- bw.write("\\r" + "=" + tm.get(key));
- break;
- default:
- bw.write(key + "=" + tm.get(key)); //
- break;
- }
- bw.newLine();
- }
- bw.close();
- }
- }
复制代码
|
|