黑马程序员技术交流社区

标题: 面试的题目分享 [打印本页]

作者: Rain2692    时间: 2014-12-13 20:43
标题: 面试的题目分享
把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如:
        a:  21 次
        b:  15 次
        c: 15 次
        把:  7 次
        当:  9 次
        前:  3 次
        ,:30 次

  1. public static void main(String[] args) throws Exception{
  2.                 // TODO Auto-generated method stub
  3.                
  4.                 BufferedReader bufr  = new BufferedReader(new FileReader("d:\\a.txt"));
  5.                 BufferedWriter bufw = new BufferedWriter(new FileWriter("d:\\new.txt"));
  6.                
  7.                 TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
  8.                 String line = null;
  9.                 while((line= bufr.readLine())!=null)
  10.                 {      
  11.                           //这部分代码是拷贝文件
  12.                           bufw.write(line);
  13.                           bufw.newLine();
  14.                           bufw.flush();
  15.                            //以下是统计每个出现的字符数
  16.                            char[] arr = line.toCharArray();
  17.                            int i=0;
  18.                        while(i<arr.length)
  19.                        {
  20.                                if(!tm.containsKey(arr[i]))//判断是否包含该字符
  21.                                                  tm.put(arr[i],1);      //不存在,则置1
  22.                                          else
  23.                                          {                         //存在则取出统计的数,并操作
  24.                                                  int count = tm.get(arr[i]);
  25.                                                  count++;
  26.                                                  tm.put(arr[i],count);//这点注意直接覆盖了,因为值是相同的
  27.                                          }
  28.                                i++;
  29.                        }
  30.                        
  31.                 }
  32.                
  33.                 BufferedWriter bufwr = new BufferedWriter(new OutputStreamWriter(System.out));
  34.                 Set<Map.Entry<Character, Integer>> entrySet = tm.entrySet();
  35.                 Iterator<Map.Entry<Character, Integer>> it = entrySet.iterator();
  36.                 while(it.hasNext())
  37.                 {
  38.                         Map.Entry<Character, Integer> me = it.next();
  39.                         char key = me.getKey();
  40.                         int num = me.getValue();
  41.                         bufwr.write(key+":"+num+"次");
  42.                         bufwr.newLine();
  43.                         bufwr.flush();
  44.                        
  45.                 }

  46.         }
复制代码


作者: 马个了黑_邓    时间: 2014-12-13 20:43
哥们哪期?
作者: Rain2692    时间: 2014-12-13 20:44
先占一个。。。
作者: 被风扬起的沙    时间: 2014-12-13 21:17
现在还看不懂,但是坚信以后会懂得
作者: wez924612    时间: 2014-12-13 21:26
受用了!准备去面试了。
作者: 魏文杰    时间: 2014-12-13 21:37
看不懂 。。。。
作者: Honelyboy    时间: 2014-12-13 22:06
看不懂,等学到这儿了再来。
作者: 何桥    时间: 2014-12-13 22:52
楼主可以啊

作者: d7142947    时间: 2014-12-13 22:55

支持   感谢  分享
作者: zhouli_2014    时间: 2014-12-13 23:12
感谢 分享
作者: 红楼    时间: 2014-12-13 23:22
膜拜。。。。。。。。。。。
作者: ben_vs_cong    时间: 2014-12-13 23:26
谢谢分享
作者: 黑马-李权88    时间: 2014-12-13 23:26
感谢前辈分享,谢谢咯,别删帖哦
作者: zzn2508    时间: 2014-12-13 23:58
感谢分享
作者: 木乃伊    时间: 2014-12-14 00:38
哥们牛逼:sleepy:
作者: 0_TNT_0    时间: 2014-12-14 01:18
厉害啊,膜拜
作者: 默默丶    时间: 2014-12-14 08:52
不关流吗   




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2