把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出
2014-07-25 17:11
337 查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package com.CSDN;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /* * 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如: * a: 21 次 * b: 15 次 * c:: 15 次 * 把: 7 次 * 当: 9 次 * 前: 3 次 * ,:30 次 * 思路: * 1.定义一个返回值 类型为Map<Character, Integer>的方法形参是str(String), * 来统计字符次数 * 2.通过IO流的读取和写入数据 * 把当前文件中的所有文本拷贝,存入一个txt文件 * 并遍历元素 */public class Test5 { public static void main(String[] args) { // 定义输入流,输出流 BufferedReader br = null; BufferedWriter bw = null; try { br = new BufferedReader(new FileReader("src\\com\\CSDN\\Test_8.java")); bw = new BufferedWriter(new FileWriter("src\\com\\CSDN\\Test_8.txt")); String res = null; StringBuffer sb = new StringBuffer(); while((res = br.readLine()) != null) { // 添加字符串 sb.append(res); // 写入数据 bw.write(res); // 换行 bw.newLine(); // 刷新流数据 bw.flush(); } Map m = countChar(sb.toString()); Set s = m.entrySet(); // 使用迭代器迭代 Iterator it = s.iterator(); // 判断是否存在下一个元素,遍历写入数据 while(it.hasNext()) { Entry<Character, Integer> entry = (Entry<Character, Integer>)it.next(); // System.out.println(entry.getKey() + ": " + entry.getValue() + "次"); bw.write(entry.getKey() + ": " + entry.getValue() + "次"); bw.newLine(); bw.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }/* * 统计字符次数 */ public static Map<Character, Integer> countChar(String str) { // 创建一个Map集合 Map<Character, Integer> map = new HashMap<Character, Integer>(); char[] ch = new char[str.length()]; str.getChars(0, str.length(),ch, 0); //增强for循环进行遍历 for(char c : ch) { Integer count = map.get(c); if(count == null) { map.put(c, 1); } else { map.put(c, ++count); } } return map; } } |
相关文章推荐
- 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出例如: a: 21 次 b: 15 次 c:: 15 次 把: 7 次 当: 9 次 前: 3 次
- 从文件读取包含数字和字母字符串,统计每个字符出现的次数,将次数输出到另外一个文件
- 第13周-项目1-小玩文件-用键盘输入文件名,统计输出文件中每个字母、数字字符出现的次数
- 获取一个文本上每个字符出现的次数,将结果写在times.txt上
- Java实现读取键盘输入保存到txt文件,再统计并输出每个单词出现次数的方法
- 针对一个文件 统计其中的单词和字符数 找出文件中出现次数最多的单词数 将文件中出现的单词按频率进行排序并输出
- 获取一个文本上每个字符出现的次数,将结果写在自定义的文本上(times.txt)
- 统计一个字符串中每个字符出现的次数,并按自然顺序输出
- 读取一个文件中的字符,统计每个字符出现的次数
- C++统计一个文件中每个数字字符(0~9)出现的次数
- 手动输入一个字符串,仅限小写字母,统计并输出每个字符在字符串中出现的次数,并输出。
- java 读取键盘输入到txt文件,统计每个单词出现的次数并输出
- java读取txt文件,并统计每行中每个字母出现的次数,并将产生的数字保存到一个新的txt文件中(有加分)
- 统计文件中各种字符(键盘上的所有可显示字符和换行符)出现的次数,文件格式为 .txt
- 统计一段文字中每个字符出现的次数,并输出结果(包含字符及其出现次数)保存在一个适合的集合中。
- 编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数
- 统计字符串”abadcdffbaeba”中每个字符出现了多少次,按次数排序并输出
- Java笔记13:统计文件中每个字符出现的次数
- 编写一个程序统计输入字符串中:各个数字,空白字符,以及其他所有字符常出现的次数。
- 线性时间内统计一个字符串中每个字符出现的次数