黑马程序员技术交流社区

标题: 关于字符的问题 [打印本页]

作者: 张昶    时间: 2013-7-14 11:46
标题: 关于字符的问题
本帖最后由 张昶 于 2013-7-15 09:53 编辑

/*
        8、 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如:
        a:  21 次
        b:  15 次
        c: 15 次
                     把:  7 次
                     当:  9 次
                     前:  3 次
                     ,:30 次
*/


package com.itheima;

import java.util.ArrayList;
import java.io.*;

public class Test_08 {
        public static void Copy() throws IOException {
                String from = "E:/in.txt";
        String to = "E:/out.txt";
        FileInputStream in = null;
        FileOutputStream out = null;
                InputStreamReader isr = null;
                //用于包装InputStreamReader,提高处理性能。因为BufferedReader有缓冲的,而InputStreamReader没有。
                BufferedReader br = null;
        try{
            in = new FileInputStream(from);
            out = new FileOutputStream(to);
            int i = 0;
            while((i = in.read()) != -1){
                out.write(i);
            }
            String str = "";
                    String str1 = "";
                    in = new FileInputStream(to);
                    // 从文件系统中的某个文件中获取字节
                    isr = new InputStreamReader(in);        // InputStreamReader 是字节流通向字符流的桥梁,
                    br = new BufferedReader(isr);                // 从字符输入流中读取文件中的内容,封装了一个new InputStreamReader的对象
                    while ((str = br.readLine()) != null) {
                            //Count(str1 += str + "\n");
                            if(str!="\n") {
                                    Count(str1 += str);
                            } else {
                                    continue;
                            }
                    }
        } catch (NullPointerException e) {
                         System.out.println("空指向异常!");
        } catch (FileNotFoundException e) {
                         System.out.println("找不到指定文件");
                } catch (IOException e) {
                         System.out.println("读取文件失败");
                } finally {
                        // 关闭的时候最好按照先后顺序关闭最后开的先关闭所以先关s,再关n,最后关m
                        br.close();
                        isr.close();
                        out.close();
                        in.close();
                }
        }
                  
        public static void Count(String tempchar) {
                ArrayList<Character> al = new ArrayList<Character>();
                for (int i = 0; i < tempchar.length(); i++) {
                        al.add(i, tempchar.charAt(i));
                }
                while (al.size()!=0) {
                        int count = 1;
                        for (int j = 1; j < al.size() ; j++) {
                                if (al.get(0).equals(al.get(j))) {
                                        al.remove(j);
                                        count++;
                                }
                        }
                        System.out.println(al.get(0)+":"+count+"次");
                        al.remove(0);
                }
        }
        
        public static void main(String[] args) throws IOException {
                Test_08 test = new Test_08();
                test.Copy();
        }
}

我得的结果:前面有一段时重复的,求高手帮解决。


作者: meng    时间: 2013-8-26 17:24
package com.itheima;

import java.io.*;
import java.util.*;

/*
5、 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如:
a:  21 次
b:  15 次
c:: 15 次
把:  7 次
当:  9 次
前:  3 次
,:30 次

思路:

1. 首先是字节流读取所有文件内容
2.考虑效率,定义缓冲区
3,定义输出流,将读取到的数据存入缓冲区,同时将文件写在磁盘上
4,将缓冲区中的数据变成字符串,方便操作
5,用字符串的替换方法,替换掉某个字符,替换后得到一个新字符串,
        原来字符串长度减去新字符串长度即为该字符出现的次数
*/
public class Test5 {

        public static void main(String[] args) {
                try {
                        test("Demo.java");
                } catch (Exception e) {

                        e.printStackTrace();
                }
        }

        public static void test(String filePath) throws Exception {

                File file = new File(filePath);
                FileInputStream fis = new FileInputStream(file);//文件输入流,读取文件
                StringBuffer sb = new StringBuffer();//定义缓冲区
                FileOutputStream fos = new FileOutputStream("out.txt");//文件输出流

                int leng = 0;
                byte[] b = new byte[1024];
                while ((leng = fis.read(b)) != -1) {//将读取到的文件添加到缓冲区内,并写入文件输出流中
                        sb.append(new String(b, 0, leng));
                        fos.write(b);
                }
                fis.close();//关闭流,释放资源
                fos.close();
                String txt = sb.toString();//将缓冲区数据变成字符串
                for (int i = 0; i < txt.length(); i++) {
                        String str = txt.substring(txt.length() - 1); // 从后往前 一个一个取字符
                        String newStr = "";
                        try {
                                newStr = txt.replaceAll(str, ""); // 把这个字符都替换掉

                        } catch (java.util.regex.PatternSyntaxException e) {

                                newStr = txt.replaceAll("\\" + str, ""); // 主要解决特殊的正则关键字
                        }
                        System.out.println(str + " 个数:" + (txt.length() - newStr.length())); // 取替换前和替换后少了多少字,得出本字符数量
                        txt = newStr;
                }
        }

}
作者: 张昶    时间: 2013-11-9 22:57
谢谢哈!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2