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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© nxp1986 中级黑马   /  2014-10-27 22:28  /  1717 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  在论坛上找了许多的入学测试题,其中一道怎么做都做不来了,太打击自信心了,有大神给点经验没

把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,

例如:

        a:  21 次
        b:  15 次
        c: 15 次
        把:  7 次
        当:  9 次
        前:  3 次
        ,:30 次


我把文件输出然后存储到缓冲里面StringBuffer里面,在转成字符数组,使用集合,但是map.get(char[i])都是null;
如果进行判断的话,就会导致打印出来的结果很明显是不一样的

8 个回复

倒序浏览
这道题我自己也研究过,贴上我的代码,你可以参考一下
  1. package reviewCode2;

  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Set;
  11. import java.util.TreeMap;

  12. /**
  13. * 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出
  14. *@author
  15. */
  16. public class IOTest {

  17.         public static void main(String[] args) throws IOException {
  18.                 File socfile = new File("G:\\BaiduYunDownload\\JavaEE\\MyWork\\reviewCode\\src\\reviewCode");
  19.                 File destfile = new File("G:\\BaiduYunDownload\\JavaEE\\MyWork\\reviewCode\\src\\reviewCode2\\my.txt");
  20.                 Myread(socfile,destfile);
  21.         }

  22.         public static void Myread(File socfile,File destfile) {
  23.                 List<File> list = new ArrayList<File>();
  24.                
  25.                 if(!socfile.exists()){
  26.                         throw new RuntimeException("非法路径");
  27.                 }else if(!socfile.isDirectory()){
  28.                         throw new RuntimeException("非法目录");
  29.                 }
  30.                
  31.                 //获取目标目录集合
  32.                 findFile(socfile,list);
  33.                
  34.                 FileReader fr = null;
  35.                 FileWriter fw = null;
  36.                
  37.                 //将每一个字符放到map集合
  38.                 Map map = new TreeMap();
  39.                 try {
  40.                         //将目录下的所有文件复制到同一个txt文件下
  41.                         for (Iterator<File> it = list.iterator(); it.hasNext();) {
  42.                                 File file = it.next();
  43.                                 fr = new FileReader(file);
  44.                                 fw = new FileWriter(destfile,true);

  45.                                 int ch = 0;
  46.                                 while ((ch = fr.read()) != -1) {
  47.                                        
  48.                                         if(map.get((char)ch)==null){
  49.                                                 map.put((char)ch,1);
  50.                                         }else{
  51.                                                 int i = (int)map.get((char)ch);
  52.                                                 map.put((char)ch,++i);
  53.                                         }
  54.                                        
  55.                                         fw.write(ch);
  56.                                         fw.flush();
  57.                                 }
  58.                         }
  59.                        
  60.                         //统计字符出现的次数
  61.                         Set<Map.Entry<?,?>> set = map.entrySet();
  62.                         for(Map.Entry<?,?> me:set){
  63.                                 System.out.println(me.getKey()+":"+me.getValue()+"次");
  64.                         }
  65.                        
  66.                 } catch (Exception e) {
  67.                         System.out.println("复制文件发生错误");
  68.                 }finally{
  69.                         if(fr!=null){
  70.                                 try {
  71.                                         fr.close();
  72.                                 } catch (IOException e) {
  73.                                         throw new RuntimeException("关闭资源异常");
  74.                                 }
  75.                                
  76.                         }
  77.                         if(fw!=null){
  78.                                 try {
  79.                                         fw.close();
  80.                                 } catch (IOException e) {
  81.                                         throw new RuntimeException("关闭资源异常");
  82.                                 }
  83.                         }
  84.                 }
  85.                
  86.         }
  87.        
  88.         //查找目录下的所有文件
  89.         public static void findFile(File socfile,List<File> list) {
  90.                 File[] files = socfile.listFiles();
  91.                 for(File f:files){
  92.                         if(f.isDirectory()){
  93.                                 findFile(f,list);
  94.                         }else{
  95.                                 list.add(f);
  96.                         }
  97.                 }
  98.         }

  99. }
复制代码
回复 使用道具 举报
没有源代码,谁也不知道你的问题。
回复 使用道具 举报
这是main方法 我直接读文件,复制部分代码没写;
  1. public static void main(String[] args)throws IOException
  2.         {
  3.                 String dest = "E:\\MyEclipse 10\\heima\\src\\FileCharacterCount.java";
  4.                 TreeMap<Character,Integer> map = new TreeMap<Character,Integer>();
  5.                 BufferedReader br = new BufferedReader(new FileReader(dest));
  6.                 String total = null;
  7.                 String line = null;
  8.                 while((line = br.readLine())!=null)
  9.                         total = total+line;
  10.                 br.close();
  11.                 char[] arr = total.toCharArray();
  12.                 for(Character c : arr)
  13.                 {
  14.                         if(map.containsKey(c))
  15.                                 map.put(c, map.get(c)+1);
  16.                         map.put(c, 1);
  17.                 }
  18.                         for(Entry<Character,Integer> entry : map.entrySet())
  19.                         {
  20.                                 System.out.println(entry.getKey()+":"+entry.getValue()+"次");
  21.                         }
  22.         }
复制代码
回复 使用道具 举报
围观{:3_65:}
回复 使用道具 举报
第一次写到这道题,跟着思路走的,没有好好封装,代码可能有些臃肿,也没有好好简化,敬请谅解

我用的是序列流,因为题意是要把文件中好几个文本写入到一个文本中,所以就使用到了序列流,详情请看代码:
public static void main(String[] args) throws Exception {
                // TODO Auto-generated method stub
                method();
        }
        public static void method() throws Exception{
                //定义一个容器,因为只有Vector有Enumeration
                Vector v = new Vector();
               
                File file = new File("c:\\2");//源文件夹
                File[] files = file.listFiles();//获取文件夹中所以的文件
                Map<Object,Integer> map = new TreeMap<Object,Integer>();//定义一个Map集合,用来获取字符出现的次数
                //遍历File数组,把文件中的文本通过文件读取流读取,并添加到集合中(要获取文本的绝对路径的名字)
                for(File newfile : files){
                        v.add(new FileInputStream(newfile.getAbsolutePath()));
                }
                //
                Enumeration en = v.elements();
                //因为文本中有字符流,所以使用了字符流缓冲区(如果用字节流的话,字符会转换不成功)
                BufferedReader br =
                                new BufferedReader(new InputStreamReader(new SequenceInputStream(en)));
                BufferedWriter bw =
                                new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\3.txt")));
                int len=0;
                //byte[] by = new byte[1024];
                int count=0;
                //读取序列流中的文本
                while((len=br.read())!=-1){
                        //得到所读字符在集合中的值
                        Integer value = map.get((char)len);
                        if(value!=null)
                                count=value;
                        count++;
                        //把字符和出现的次数存入到集合中
                        map.put((char)len, count);
                        count=0;
                        //把读取缓冲区中的数据写入到目的文本中
                        bw.write(len);
                }
                //遍历Map集合,得到文本中字符相对应的出现次数
                br.close();
                bw.close();
                Set<Map.Entry<Object, Integer>> set = map.entrySet();
                Iterator<Map.Entry<Object, Integer>> it = set.iterator();
                while(it.hasNext()){
                        Entry<Object, Integer> me = it.next();
                        System.out.println(me.getKey()+".."+me.getValue());
                }
        }
       
回复 使用道具 举报
怎一个唉字了得啊。。。
回复 使用道具 举报
加油啊 加油
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马