首页 技术 正文
技术 2022年11月20日
0 收藏 532 点赞 4,046 浏览 2166 个字

一、代码实现

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

二、测试结果

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秒。

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898