低级字节流一个一个字节复制
低级字节流按照字节数组复制
高级字节流一个一个字节复制
低级字节流按照字节数组复制
public class TimeTest4 {
//复制的图片路径
private static final String SRC_FILE = "G:\\vsCode\\images\\OIP-C.jfif";
//复制到那个目的地
private static final String DEST_FILE = "G:\\image\\";
public static void main(String[] args) {
copy1();
copy2();
copy3();
copy4();
}
//使用低级的字节流按照一个一个字节的形式复制文件
private static void copy1(){
long startTime = System.currentTimeMillis();
try(
InputStream is =new FileInputStream(SRC_FILE);
OutputStream os = new FileOutputStream(DEST_FILE+"1.jfif");
){
int b ;
while((b=is.read())!=-1){
os.write(b);
}
}catch(Exception e){
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("低级字节流一个一个字节复制耗时"+(endTime-startTime)/1000.0);
}
//使用低级的字节流按照字节数组的形式复制文件
private static void copy2(){
long startTime = System.currentTimeMillis();
try(
InputStream is =new FileInputStream(SRC_FILE);
OutputStream os = new FileOutputStream(DEST_FILE+"2.jfif");
){
byte[] buffer =new byte[50] ;
int len;
while((len=is.read(buffer))!=-1){
os.write(buffer,0,len);
}
}catch(Exception e){
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("低级字节流按照字节数组复制耗时"+(endTime-startTime)/1000.0);
}
//使用高级的缓冲字节流按照一个一个字节的形式复制文件
private static void copy3(){
long startTime = System.currentTimeMillis();
try(
InputStream is =new FileInputStream(SRC_FILE);
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os = new FileOutputStream(DEST_FILE+"3.jfif");
BufferedOutputStream bos = new BufferedOutputStream(os);
){
int i;
while((i=bis.read())!=-1){
bos.write(i);
}
}catch(Exception e){
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("高级字节流一个一个字节复制耗时"+(endTime-startTime)/1000.0);
}
//使用低级的字节流按照字节数组的形式复制文件
private static void copy4(){
long startTime = System.currentTimeMillis();
try(
InputStream is =new FileInputStream(SRC_FILE);
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os = new FileOutputStream(DEST_FILE+"4.jfif");
BufferedOutputStream bos = new BufferedOutputStream(os);
){
byte[] buffer =new byte[50] ;
int len;
while((len=bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
}catch(Exception e){
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("高级字节流按照字节数组复制耗时"+(endTime-startTime)/1000.0);
}
}