标签:
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