// 1、使用字符流一次读一个字符的方式,复制文本文件
private static void m1() throws IOException {
FileReader re = new FileReader("源文件路径");
FileWriter wr = new FileWriter("目标文件路径");
long t1 = System.currentTimeMillis();
int i = -1;
while ((i = re.read()) != -1) {
wr.write(i);
wr.flush();
}
long t2 = System.currentTimeMillis();
System.out.println("第1种方法复制共用时" + (t2 - t1) + "秒");
wr.close();
re.close();
}
// 2、使用字符流一次读一个字符数组的方式,复制文本文件
private static void m2() throws IOException {
FileReader re = new FileReader("源文件路径");
FileWriter wr = new FileWriter("C:\\Users\\LQF\\Desktop\\123\\2-2.txt");
long t1 = System.currentTimeMillis();
int i = -1;
char[] c = new char[1024];
while ((i = re.read(c)) != -1) {
wr.write(c, 0, i);
wr.flush();
}
long t2 = System.currentTimeMillis();
System.out.println("第2种方法复制共用时" + (t2 - t1) + "秒");
wr.close();
re.close();
}
// 3、使用字符缓冲流一次读一个字符的方式,复制文本文件
// BufferedWriter(Writer out)
// BufferedReader(Reader in)
public static void m3() throws IOException {
Reader in = new FileReader("源文件路径");
Writer out = new FileWriter("C:\\Users\\LQF\\Desktop\\123\\2-3.txt");
BufferedReader bin = new BufferedReader(in);
BufferedWriter bout = new BufferedWriter(out);
long t1 = System.currentTimeMillis();
int i = -1;
while ((i = bin.read()) != -1) {
bout.write(i);
bout.flush();
}
long t2 = System.currentTimeMillis();
System.out.println("第3种方法复制共用时" + (t2 - t1) + "秒");
bout.close();
bin.close();
}
// 4、使用字符缓冲流一次读字符数组的方式,复制文本文件
// BufferedWriter(Writer out)
// BufferedReader(Reader in)
public static void m4() throws IOException {
Reader in = new FileReader("源文件路径");
Writer out = new FileWriter("C:\\Users\\LQF\\Desktop\\123\\2-4.txt");
BufferedReader bin = new BufferedReader(in);
BufferedWriter bout = new BufferedWriter(out);
long t1 = System.currentTimeMillis();
int i = -1;
char[] c = new char[1024];
while ((i = bin.read(c)) != -1) {
bout.write(c, 0, i);
bout.flush();
}
long t2 = System.currentTimeMillis();
System.out.println("第4种方法复制共用时" + (t2 - t1) + "秒");
bout.close();
bin.close();
}
}