Java中字符流过滤器BufferedReader,BufferedWriter,PrinterWriter

前面的博客中没有具体地说一下过滤器,这里依旧以文件的读写举例。先直接上代码:

    public static void main(String[] args) throws IOException {

        File fileFrom = new File("e:/my.txt");
        File fileTo = new File("e:/my001.txt");
        // 读文件
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(fileFrom), "gbk"));
        // 写入
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(fileTo), "gbk"));

        //while循环边读边写
        String line=null;
        while((line=br.readLine())!=null){
            System.out.println(line);
            //写入
            bw.write(line);
            bw.flush();
        }
        //写入
        br.close();
        bw.close();

    }

注意了,这里的br有readLine的功能,可是bw是没有的,因此需要再while中改进代码,增加换行:

    while ((line = br.readLine()) != null) {
            System.out.println(line);
            // 写入
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

看bw.newLine();的api:

/**
     * Writes a line separator.  The line separator string is defined by the
     * system property <tt>line.separator</tt>, and is not necessarily a single
     * newline ('\n') character.
     *
     * @exception  IOException  If an I/O error occurs
     */
    public void newLine() throws IOException {
        write(lineSeparator);
    }

中文:

newLine
public void newLine()
             throws IOException写入一个行分隔符。行分隔符字符串由系统属性 line.separator 定义,并且不一定是单个新行 ('\n') 符。 

抛出: 
IOException - 如果发生 I/O 错误

自然就明白了。
这里改进代码:BufferedWriter换成PrinterWriter

File fileFrom = new File("e:/my.txt");
        File fileTo = new File("e:/my001.txt");
        // 读文件
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(fileFrom), "gbk"));
        // 写入
        PrintWriter pw=new PrintWriter(fileTo) ;

        // while循环边读边写
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            // 写入
            pw.println(line);
            pw.flush();
        }
        // 写入
        br.close();
        pw.close();

最后,再提取方法出来。如下:

private static void FileCopyTo(File fileFrom, File fileTo)
            throws UnsupportedEncodingException, FileNotFoundException,
            IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(fileFrom), "gbk"));
        // 写入
        PrintWriter pw=new PrintWriter(fileTo) ;

        // while循环边读边写
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            // 写入
            pw.println(line);
            pw.flush();
        }ss
        // 写入
        br.close();
        pw.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值