Java利用高效缓冲字符流来快速copy文件
代码:
package cn1;
import java.io.*;
import java.util.Date;
public class Copy1 {
public static void main(String[] args) {
//算出程序开始的时间
Date date = new Date();
long starTime = date.getTime();
BufferedReader reader = null;
BufferedWriter writer = null;
try {
//创建一个高效缓冲输入流
reader = new BufferedReader(new FileReader("hello.txt"));
//创建一个高效缓冲输出流
writer = new BufferedWriter(new FileWriter("hello1.txt"));
//创建一个读一行的变量
String line = null;
while ((line = reader.readLine()) != null){
writer.write(line);
writer.newLine();//换行
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null){
writer.close();
}
if (reader != null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Date date1 = new Date();
long endTime = date1.getTime();
long runTime = endTime - starTime;
System.out.println("程序运行了"+runTime+"毫秒");
}
}