- public class 课堂联机
- {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException
- {
- Demo2();
- }
- private static void Demo2() throws FileNotFoundException, IOException {
- BufferedReader br =
- new BufferedReader(new FileReader("day22笔记.txt"));
- BufferedWriter bw =
- new BufferedWriter(new FileWriter("times.txt"));
- Map<Character, Integer> map = new TreeMap<>();
- int ch;
- while((ch = br.read())!= -1)
- {
- char c = (char) ch;
- map.put(c, map.containsKey(c) ? map.get(c) + 1: 1 );
- }
- for (Character c : map.keySet())
- {
- switch (c) {
- case '\t':
- System.out.println("\\t" + " = " + map.get(c));
- break;
- case '\r':
- System.out.println("\\r" + " = " + map.get(c));
- break;
- case '\n':
- System.out.println("\\n" + " = " + map.get(c));
- break;
- case ' ':
- System.out.println("空格" + " = " + map.get(c));
- break;
- default:
- System.out.println(c + " = " + map.get(c));
- }
- }
- }
复制代码 这个是我在课上 一起写的代码 读入一个文本 将文本里的每个字出现了几次 打印出来
如“我 = 31”“代 = 14”“, = 45”等
但如何将这些打印出来的信息 写入我创建的文本中啊? |