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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 嘿~~ 中级黑马   /  2014-10-16 15:41  /  794 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;
  2. import java.io.*;
  3. class  GetCount
  4. {
  5.         public static void main(String[] args) throws Exception
  6.         {
  7.                 File file = new File("c:\\java\\tempfile\\count.txt");

  8.                 FileReader fr = new FileReader(file);
  9.                
  10.                 File confile = new File("c:\\java\\tempfile\\properties.ini");
  11.                
  12.                 if(!confile.exists())
  13.                         confile.createNewFile();
  14.                
  15.                 FileWriter fw = new FileWriter(confile);

  16.                 //定义集合容器,可以将集合元素排序
  17.                 List list = new ArrayList();

  18.                 int cuf = 0;
  19.                 while((cuf=fr.read())!=-1){
  20.                         //跳过空格
  21.                         if(cuf=='\r' || cuf=='\n')
  22.                                 continue;
  23.                         //将读取到的字符作为对象添加进集合中
  24.                         list.add((char)cuf);
  25.                 }
  26.                 //对集合元素排序,便于操作
  27.                 Collections.sort(list);
  28.                 int size = list.size();
  29.                
  30.                 PrintWriter pw = new PrintWriter(fw);
  31.                
  32.                 //定义临时变量存储角标
  33.                 int temp = 0;
  34.                 //定义角标
  35.                 int pos = 0;
  36.                 //定义计数器
  37.                 int count = 0;
  38.                
  39.                 while(pos != size-1){
  40.                         //某一个元素在集合中最后一次出现的角标
  41.                         pos = list.lastIndexOf(list.get(temp));
  42.                         //该元素出现的次数
  43.                         count = pos - temp+1;
  44.                         //改变第二次查询的起始位置
  45.                         temp = pos+1;
  46.                         char ch = (char)list.get(pos);
  47.                         pw.println(ch+"  : "+count+" 次");
  48.                 }
  49.                 fr.close();
  50.                 fw.close();
  51.                 pw.close();
  52.         }
  53. }



  54. 文件
  55. avrhsgj公共rhgrheh反话呢dhh24323
  56. fn/[]';;w.//',.更稳固和2397273jtbsdb】‘iasgaga【【】qtwsaga/.,';'qwqt
  57. ahgqh/'[;[]=-00++++1他23他weg248…………@#……()¥……@……!%%¥#J%^Kqtsdghw
  58. 、、。,,《》?“::PU请
  59. 广告hwehh


  60. 打印结果

  61.    : 1 次
  62. #  : 2 次
  63. %  : 3 次
  64. '  : 5 次
  65. +  : 4 次
  66. ,  : 2 次
  67. -  : 1 次
  68. .  : 3 次
  69. /  : 5 次
  70. 0  : 2 次
  71. 1  : 1 次
  72. 2  : 6 次
  73. 3  : 5 次
  74. 4  : 2 次
  75. 7  : 2 次
  76. 8  : 1 次
  77. 9  : 1 次
  78. ;  : 4 次
  79. =  : 1 次
  80. @  : 2 次
  81. J  : 1 次
  82. K  : 1 次
  83. P  : 1 次
  84. U  : 1 次
  85. [  : 3 次
  86. ]  : 2 次
  87. ^  : 1 次
  88. a  : 7 次
  89. b  : 2 次
  90. d  : 3 次
  91. e  : 3 次
  92. f  : 1 次
  93. g  : 8 次
  94. h  : 12 次
  95. i  : 1 次
  96. j  : 2 次
  97. n  : 1 次
  98. q  : 5 次
  99. r  : 3 次
  100. s  : 5 次
  101. t  : 4 次
  102. v  : 1 次
  103. w  : 6 次
  104. ‘  : 1 次
  105. “  : 1 次
  106. …  : 10 次
  107. 、  : 2 次
  108. 。  : 1 次
  109. 《  : 1 次
  110. 》  : 1 次
  111. 【  : 2 次
  112. 】  : 2 次
  113. 他  : 2 次
  114. 公  : 1 次
  115. 共  : 1 次
  116. 反  : 1 次
  117. 告  : 1 次
  118. 呢  : 1 次
  119. 和  : 1 次
  120. 固  : 1 次
  121. 广  : 1 次
  122. 更  : 1 次
  123. 稳  : 1 次
  124. 话  : 1 次
  125. 请  : 1 次
  126. !  : 1 次
  127. (  : 1 次
  128. )  : 1 次
  129. ,  : 2 次
  130. :  : 2 次
  131. ?  : 1 次
  132. ¥  : 2 次
复制代码

/*
6、 统计一个文本文件中字符出现的次数,结果存入另外的一个文本文件中。
例如: a:  21 次     b:  15   c:: 15  把:  7   当:  9 次  前:  3 次  ,:30 次
*/


1 个回复

倒序浏览
为什么我的技术帖子版主视而不见啊:'(:'(:'(
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马