java基础 字符缓冲流[BufferedReader 和 BufferedWriter]

package com.java_course.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

/**
 * 字符缓冲流
 * BufferedReader 缓冲输入流(不指定缓冲大小,默认8192字符)
 * BufferedWriter 缓冲输出流(不指定缓冲大小,默认8192字符)
 * @author Shen
 */
public class WriteExample {

	public static void main(String[] args) {
		
		try {
			copy("src/com/java_course/io/text.txt","src/com/java_course/io/text1.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 文件的复制
	 * @author Mir:Shen
	 * @param srcPath   源文件
	 * @param destPath  目标文件
	 * @throws IOException
	 */
	public static void copy(String srcPath,String destPath) throws IOException{
		
		Reader reader = new BufferedReader(new FileReader(srcPath));
		Writer writer = new BufferedWriter(new FileWriter(destPath));
		
		int len = 0;
		char[] buff = new char[1024];
		while((len = reader.read(buff)) !=-1){
			writer.write(buff, 0, len);
		}
		
		writer.flush();
		writer.close();
		reader.close();
	}
}

你可能感兴趣的:(Java)