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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lll456123l 中级黑马   /  2016-5-1 12:44  /  235 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

* 统计一个文本文件中字符出现的次数,结果存入另外的一个文本文件中。
*/
public class Text5 {

        public static void main(String[] args) throws IOException {
                FileOutputStream out=new FileOutputStream("第五题需要被统计的文本文档.txt");
                String str="fwegwegwsvsdbregwfasfqarwrqojwrgh";
        byte[] b=str.getBytes();
                out.write(b);
                out.flush();
        FileInputStream in=new FileInputStream("第五题需要被统计的文本文档.txt");
        int l;
        while((l=in.read(b))!=-1)
        {
                String se=new String(b,0,l);
                  char [] ch=se.toCharArray();
                  HashMap<Character,Integer> tree=new HashMap<Character,Integer>();
              for(int x=0;x<ch.length;x++)
              {
                      Integer value=tree.get(ch[x]);    //建立键与值的关系
                      if(value==null)
                      tree.put(ch[x], 1);
                      else
                              tree.put(ch[x], value+1);     
              }
             BufferedWriter  out1=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("第五题存放结果文本.txt")));
            Set<Character> set=tree.keySet();
            Iterator<Character> it=set.iterator();
            while(it.hasNext())
                            {
                               char temp=it.next();
                               out1.write(temp+"  :"+tree.get(temp));
                               out1.newLine();
                               out1.flush();
                               
                            }
         
        }
        
      
        }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马