package test05;
import java.io.*;
public class test080702 {
public static void main(String[] args) {
copy_1();
copy_2();
}
//第二种:以字符数组方式复制。
public static void copy_2() {
FileWriter fw=null;
FileReader fr=null;
try {
fw=new FileWriter("D:\\Java练习\\test0804_copy_2.txt");
fr=new FileReader("D:\\Java练习\\test05\\src\\test05\\test0804.java");
char[] buf=new char[1024];
int len=0;
long start=System.currentTimeMillis();
while((len=fr.read(buf))!=-1) {
fw.write(buf,0,len);
}
long end=System.currentTimeMillis();
System.out.println("数组方式耗时为:"+(end-start));
}catch(IOException e) {
throw new RuntimeException("复制失败");
}finally {
try {
if(fw!=null)
fw.close();
}catch(IOException e) {
System.out.println(e.toString());
}finally {
try {
if(fr!=null)
fr.close();
}catch(IOException e) {
System.out.println(e.toString());
}
}
}
}
//第一种方式:以单个字节方式复制。
public static void copy_1() {
FileWriter fw=null;
FileReader fr=null;
try {
fw=new FileWriter("D:\\Java练习\\test0804_copy_1.txt");
fr=new FileReader("D:\\Java练习\\test05\\src\\test05\\test0804.java");
int ch=0;
long start=System.currentTimeMillis();
while((ch=fr.read())!=-1) {
fw.write(ch);
}
long end=System.currentTimeMillis();
System.out.println("字节方式耗时为:"+(end-start));
}catch(IOException e) {
System.out.println(e.toString());
}finally {
try {
if(fw!=null)
fw.close();
}catch(IOException e) {
System.out.println(e.toString());
}
try {
if(fr!=null) {
fr.close();
}
}catch(IOException e) {
System.out.println(e.toString());
}
}
}
}