码迷,mamicode.com
首页 > 编程语言 > 详细

Java IO - BufferedReader & BufferedWriter

时间:2016-05-01 00:57:49      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

java.io 包提供丰富的 IO 读写功能,封装在不同的类里面。其中,使用 BufferedReader 和 BufferedWriter 可以方便地进行读、写文件的操作。

使用例子如下:

import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class FileReaderExample {

    public final static String PATH_IN = "src/resources/data_input.txt";
    public final static String PATH_OUT = "src/resources/data_output.txt";

    /**
     * example of BufferedReader and BufferedWriter
     * 
     * @throws Exception
     */
    public void BufferedReader_Writer() throws Exception {

        FileReader fr = new FileReader(PATH_IN);
        BufferedReader br = new BufferedReader(fr);

        FileWriter fw = new FileWriter(PATH_OUT);
        BufferedWriter bw = new BufferedWriter(fw);

        String line;
        while ((line = br.readLine()) != null) {
            out.println(line);
            bw.write(line + "\n");
        }

        String endStr = " - End of BufferedReader ";
        out.print(endStr);
        bw.write(endStr + "\n");

        br.close();
        bw.close();
    }
}

 

参考资料:

Java IO BufferedReader, tutorialspoint

Java Files and IO, tutorialspint

Java IO - BufferedReader & BufferedWriter

标签:

原文地址:http://www.cnblogs.com/TonyYPZhang/p/5449767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!