java 缓冲字符流

package 缓冲字符;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 缓冲字符流,优点:是读取写入高效;缺点:不能直接操作文件
 */
public class BufferedReaderDemo {

	public static void main(String[] args) {
		File file = new File("a.txt");
		copy(file);
		System.out.println("ok");

	}

	public static void copy(File file) {
		BufferedReader bReader = null;
		BufferedWriter bWriter = null;
		try {
			bReader = new BufferedReader(new FileReader(file));
			bWriter = new BufferedWriter(new FileWriter("dd.txt"));
			String line = null;
			// 每次读取一行
			while ((line = bReader.readLine()) != null) {
				// 每次写入一行
				bWriter.write(line);
				// 换行
				bWriter.newLine();
				bWriter.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bReader != null) {
				try {
					bReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (bWriter != null) {
				try {
					bWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
}

你可能感兴趣的:(java,IO,java,基础)