Java笔记13:统计文件中每个字符出现的次数

一、代码实现

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. import java.io.*;  
  2. import java.util.*;  
  3.   
  4. /** 
  5. 功能:统计文件中每个字符出现的次数 
  6. 思路: 
  7. 1.定义字符读取(缓冲)流 
  8. 2.循环读取文件里的字符,用一个String类型变量接收(newValue) 
  9. 3.把newValue变成字符数组       char[] ch = newValue.toCharArray(); 
  10. 4.遍历ch,将ch中所有的字符存入一个Map集合中(TreeSet),键对应字符,值对应字符出现的次数 
  11. 5.遍历打印map集合中的键和值,也就是字符出现的次数 
  12. **/  
  13. public class Stat {  
  14.     public static void main(String[] args) {  
  15.         long startTime = System.currentTimeMillis();  
  16.         stat(new File("e:\input.txt"));  
  17.         long endTime = System.currentTimeMillis();  
  18.       
  19.         System.out.println(" 运行时间:" + (endTime - startTime) + "毫秒");  
  20.     }  
  21.   
  22.     public static void stat(File file){  
  23.         BufferedReader bfr = null;                          //定义字符读取(缓冲)流  
  24.         try{  
  25.             bfr = new BufferedReader(new FileReader(file)); //给该流赋值  
  26.             String value = null;                            //定义一个临时接收文件中的字符串变量  
  27.             String newValue = "";                           //接收文件中所有字符串的变量  
  28.             while((value = bfr.readLine()) != null){        //开始读取文件中的字符  
  29.                 newValue = newValue + value;                //存入newValue变量中  
  30.             }  
  31.             char[] ch = newValue.toCharArray();             //把newValue变成字符数组  
  32.             TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>(); //定义一个TreeMap,默认从小到大顺序,键对应字符,值对应字符出现的次数  
  33.             for(int x = 0;x < ch.length; x++){               //遍历ch,将ch中所有的字符存入一个Map集合中(TreeSet),键对应字符,值对应字符出现的次数  
  34.                 char c = ch[x];  
  35.                 if(tm.containsKey(c)){                      //如果TreeMap(tm)中有该键,则取出该键中的值,也就是出现的次数  
  36.                     int count = tm.get(c);  
  37.                     tm.put(c, count + 1);                   //把新值存入tm集合中,如果键相同的话, 新键会替换老键,值也随着变化了  
  38.                 }  
  39.                 else{  
  40.                     tm.put(c, 1);                           //如果没有出现该键就说明是第一次出现,存入1次  
  41.                 }  
  42.             }  
  43.             //下面的是取出TreeMap(tm)中的键和值  
  44.             Set<Map.Entry<Character, Integer>> set = tm.entrySet();  
  45.             Iterator<Map.Entry<Character, Integer>> iter = set.iterator();  
  46.             while(iter.hasNext()){  
  47.                 Map.Entry<Character, Integer> map = iter.next();  
  48.                 char k = map.getKey();  
  49.                 int v = map.getValue();  
  50.                 System.out.print(k + "(" + v + "次)  ");  
  51.             }  
  52.         }  
  53.         catch(IOException e){  
  54.             System.out.println("文件读取错误");  
  55.         }  
  56.         finally{  
  57.             try{  
  58.                 if(bfr!=null)  
  59.                     bfr.close();  
  60.             }  
  61.             catch(IOException e){  
  62.                 System.out.println("文件关闭错误");  
  63.             }  
  64.         }  
  65.     }  
  66. }  

二、测试结果

1 在E:input.txt中输入两行数据:

Hello World!

您好世界!

运行结果为:

(1次) !(1次) H(1次) W(1次) d(1次) e(1次) l(3次) o(2次) r(1次) 世(1次) 好(1次) 您(1次) 界(1次) !(1次) ,(1次

运行时间:1毫秒

注:上面第1个没显示出来的字符是空格。

2 在E:inut.txt中多放些数据,比如放了1.2M的数据,运行时间是3秒。

    这里可以看出,这个算法仅仅是实现了最基本需求,当文件很大时,需要很长的时间才能得出结果。如果是在真实项目里,需要提高此算法的效率,或改用其它思路来实现。

原文地址:https://www.cnblogs.com/grimm/p/6732471.html