import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main (String[]args){
long startTime=System.currentTimeMillis();
try{
FileInputStream fis=new FileInputStream("a.jpg");
FileOutputStream fos=new FileOutputStream("temp.jpg");
int read=fis.read();
while(read!=-1){
fos.write(read);
read=fis.read();
}
fis.close();
fos.close();
}
catch(IOException e){
e.printStackTrace();
}
long endTime=System.currentTimeMillis();
System.out.println("程序运行时间:"+(endTime-startTime)+"ms");
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main (String[]args){
long startTime=System.currentTimeMillis();
try{
FileInputStream fis=new FileInputStream("a.jpg");
FileOutputStream fos=new FileOutputStream("temp.jpg");
byte[]buffer=new byte[1024];
int read=fis.read(buffer);
while(read!=-1){
fos.write(read);
read=fis.read();
}
fis.close();
fos.close();
}
catch(IOException e){
e.printStackTrace();
}
long endTime=System.currentTimeMillis();
System.out.println("程序运行时间:"+(endTime-startTime)+"ms");
}
}
可以看到添加代码后让原本缓慢的读写变得高效率起来,由原来的508ms变成485ms