A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ╰繼/aiq戀い 初级黑马   /  2014-6-27 09:54  /  801 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如:

        a:  21 次
        b:  15 次
        c:: 15 次
        把:  7 次
        当:  9 次
        前:  3 次
        ,:30 次

1 个回复

倒序浏览
本帖最后由 496080891 于 2014-6-27 14:59 编辑
  1. public class Test6 {

  2.         /**
  3.          * 6、 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,
  4.          * 例如:
  5.       *  a:  21 次
  6.       *  b:  15 次
  7.       *  c:: 15 次
  8.            *          把:  7 次
  9.           *  当:  9 次        
  10.           *  前:  3 次
  11.           *  ,:30 次
  12.          * @param args
  13.          * @throws IOException
  14.          */
  15.         public static void main(String[] args) throws IOException {
  16.                 File file1 = new File("D:\\eclipse\\exam\\src\\com\\itheima\\Test6.java");//关联当前文件
  17.                 File file2 = new File("D:\\eclipse\\exam\\src\\com\\itheima\\Test6.txt");//关联目标文件
  18.                 BufferedReader in =
  19.                                 new BufferedReader(new FileReader(file1));//输入流                                       
  20.                 BufferedWriter out =
  21.                                 new BufferedWriter(new FileWriter(file2));//输出流
  22.                 String len = null;
  23.                 StringBuffer sb = new StringBuffer();
  24.                 while ((len=in.readLine())!=null){
  25.                         sb.append(len);
  26.                         out.write(len);
  27.                         out.newLine();
  28.                         out.flush();
  29.                 }
  30.                 Map map = countChar(sb.toString());//创建Map集合
  31.                 Set set = map.entrySet();
  32.                 Iterator it = set.iterator();
  33.                 while(it.hasNext()){//循环写入Key和Value的值
  34.                         Entry<Character,Integer> entry = (Entry<Character, Integer>) it.next();
  35.                         //System.out.println(entry.getKey()+":"+entry.getValue()+"次");
  36.                         out.write(entry.getKey()+":"+entry.getValue()+"次");
  37.                         out.newLine();
  38.                         out.flush();
  39.                 }
  40.                 in.close();
  41.                 out.close();
  42.         }
  43.         //统计字符出现次数;
  44.         public static Map<Character,Integer> countChar(String str){
  45.                 Map<Character,Integer> map = new HashMap<Character,Integer>();
  46.                 char[] chars = new char[str.length()];
  47.                 str.getChars(0, str.length(), chars, 0);
  48.                 for(char c : chars){
  49.                         Integer count = map.get(c);
  50.                         if(count == null)
  51.                                 map.put(c, 1);
  52.                         else
  53.                                 map.put(c, ++count);
  54.                 }
  55.                 return map;
  56.         }

  57. }
复制代码
回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马