BufferedReader、BufferedWriter
同缓冲字节流一致,利用缓存机制,大大提高了读写文本文件的效率。
BufferedReader新增了逐行读取方法 readLine();
BufferedWriter新增了换行方法 newLine();
import java.io.*;public class BufferedReaderTest {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("Garden.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("Garden-copy.txt"));copy(br, bw);}public static void copy(BufferedReader br, BufferedWriter bw) {
String str = null;try {
//读取一整行while ((str = br.readLine()) != null) {
bw.write(str);bw.newLine(); //换行}bw.flush();} catch (IOException e) {
e.printStackTrace();} finally {
try {
if (null != bw) {
bw.close();}} catch (IOException e) {
e.printStackTrace();}try {
if (null != br) {
br.close();}} catch (IOException e) {
e.printStackTrace();}}}
}