s-day09--字节缓冲流一次读写一个字节数组的方式复制文件

This commit is contained in:
xx
2026-10-23 15:51:14 +08:00
parent 456a4be960
commit 44035052dd
@@ -0,0 +1,55 @@
package com.inmind.buffered01;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/*
使用缓冲流,一次读写一个字节数组
*/
public class BufferedDemo02 {
//6ms
public static void main(String[] args) throws Exception {
//直接使用缓冲流来实现文件复制(一次读写一个字节数组)
//直接使用默认的字节流来实现文件复制
FileInputStream input = new FileInputStream("a.jpg");
FileOutputStream output = new FileOutputStream("b4.jpg");
BufferedInputStream fis = new BufferedInputStream(input);
BufferedOutputStream fos = new BufferedOutputStream(output);
long start = System.currentTimeMillis();
//一边读一边写
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
long end = System.currentTimeMillis();
fis.close();
fos.close();
System.out.println("文件复制所需时间:"+(end -start));
}
//20ms
public static void method(String[] args) throws Exception {
//直接使用默认的字节流来实现文件复制
FileInputStream fis = new FileInputStream("a.jpg");
FileOutputStream fos = new FileOutputStream("b3.jpg");
long start = System.currentTimeMillis();
//一边读一边写
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
long end = System.currentTimeMillis();
fis.close();
fos.close();
System.out.println("文件复制所需时间:"+(end -start));
}
}