文件复制的步骤:
1、创建字节缓冲输入流对象,构造方法中传递字节输入流
2、创建字节缓冲输出流对象,构造方法中传递字节输出流
3、使用字节缓冲输入流对象中的read方法,读取文件
4、使用字节缓冲输出流中的方法write,把读取到的数据写入带内部缓冲区中
5、释放资源
package com.baidu.Buffered;
import java.io.*;
public class 文件复制字节缓冲流 {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
BufferedInputStream bis= new BufferedInputStream(new FileInputStream("C:\\1.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\develop\\1.jpg"));
int len = 0;
while ((len = bis.read())!=-1){
bos.write(len);
}
bos.close();
bis.close();
long e = System.currentTimeMillis();
System.out.println("复制文件耗时"+(e-s)+"毫秒");
}
}
45毫秒
package com.baidu.Buffered;
import java.io.*;
public class 文件复制字节缓冲流 {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
BufferedInputStream bis= new BufferedInputStream(new FileInputStream("C:\\1.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\develop\\1.jpg"));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
long e = System.currentTimeMillis();
System.out.println("复制文件耗时"+(e-s)+"毫秒");
}
}
10毫秒