JAVA之用缓冲流读写文件

public class CopyDemo {
public static void main(String[] args) throws Exception{
long time1 = System.currentTimeMillis();
copy4(new File("d:\ccc.mp4"),new File("e:\ccc.mp4"));
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
}
//1.字节流读写单个字节 太慢了
public static void copy1(File src,File desc) throws Exception{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0;
while ((len=fis.read())!=-1){
fos.write(len);
}
fos.close();
fis.close();
}
//2.字节流读写字节数组 550
public static void copy2(File src,File desc) throws Exception{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0;
byte[] b = new byte[1024*10];
while ((len=fis.read(b))!=-1){
fos.write(b,0,len);
}
fos.close();
fis.close();
}
//3.字节流缓冲区 读写单个字节 4252
public static void copy3(File src,File desc) throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0;
while ((len=bis.read())!=-1){
bos.write(len);
}
bis.close();
bos.close();
}
//4.字节流缓冲区读写字节数组 585
public static void copy4(File src,File desc) throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0;
byte[] b = new byte[1024*10];
while ((len=bis.read(b))!=-1){
bos.write(b,0,len);
}
bos.close();
bis.close();
}
原文地址:https://www.cnblogs.com/Y-mmeng/p/10606464.html