BufferedReader缓存流应用

本文介绍了如何利用BufferedReader按行读取文本文件,并检查其中的敏感词汇,将其替换为*,然后将处理后的文本写入新文件。敏感词汇存储在数组或集合中,通过循环读取文件内容,逐行进行替换操作,最终关闭流对象释放资源。

将文本中的所有的敏感词汇替换成*
按行读取文本文件,读取到的字符串中查找是否有敏感词汇,如果有就替换成*,并将这一行字符串写到新的文件中.
敏感词汇自定义
1.创建一个字符输入流和含有脏字的文件相关联,因为要按行读取,所以用BufferedReader,readLine
2.敏感词汇有多个,所以用数组或者是集合存储起来
3.因为要将替换脏字后的每行内容写到一个新的文件中,所以创建字符输出流对象,用BufferedWriter
4.循环读取文件内容
(1)获取当前行
(2)将当前行中的脏字替换成
(3)将过滤脏字后的内容写入到新的文件中
5.关闭流对象,释放资源`


public static void main(String[] args) throws IOException {
        //1.创建一个字符输入流和含有脏字的文件相关联,因为要按行读取,所以用BufferedReader,readLine
        BufferedReader br = new BufferedReader(new FileReader("old段子.txt"));
        //2.敏感词汇有多个,所以用数组或者是集合存储起来
        String[] strs = {"别傻了","程序猿","我艹","死光光"};
        //3.因为要将替换脏字后的每行内容写到一个新的文件中,所以创建字符输出流对象,用BufferedWriter
        BufferedWriter bw = new BufferedWriter(new FileWriter("新段子.txt"));
        //4.循环读取文件内容
        String line;
        while((line=br.readLine())!=null){

            //因为脏字有多个,而且存在数组中,所以遍历数组
            for(int i=0;i<strs.length;i++){
                line = line.replace(strs[i], "***");//字符串内容是不可改变的,所以你要把返回的新字符串从新赋给line
                //(这里打错了)
            }
            bw.write(line);
            bw.newLine();
        }
        br.close();
        bw.close();
    }
public static String loadAFileToStringDE1(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( new FileInputStream(f) ); long contentLength = f.length(); ByteArrayOutputStream outstream = new ByteArrayOutputStream( contentLength > 0 ? (int) contentLength : 1024); byte[] buffer = new byte[4096]; int len; while ((len = is.read(buffer)) > 0) { outstream.write(buffer, 0, len); } outstream.close(); ret = outstream.toString(); //byte[] ba = outstream.toByteArray(); //ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法1用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE2(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new FileInputStream(f) ; long contentLength = f.length(); byte[] ba = new byte[(int)contentLength]; is.read(ba); ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法2用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE3(File f) throws IOException { long beginTime = System.currentTimeMillis(); BufferedReader br = null; String ret = null; try { br = new BufferedReader(new FileReader(f)); String line = null; StringBuffer sb = new StringBuffer((int)f.length()); while( (line = br.readLine() ) != null ) { sb.append(line).append(LINE_BREAK); } ret = sb.toString(); } finally { if(br!=null) {try{br.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法3用时"+ (endTime-beginTime) + "ms"); return ret; } 3个方法去读取一个大于50M的文件,当不设置jvm参数时都OutofMemery,当设置-Xmx128M时。只有方法3 可以通过,设置到-Xmx256M时也只有方法3可以通过,干脆设置512M,都可以了,运行时间如果正常的话一般都在4~5S
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值