缓冲流复制文件与基本流复制文件比较

import javafx.scene.control.ButtonBar;

import java.io.*;

public class Demo03Copy2 {
    public static void main(String[] args) throws IOException {
        long s=System.currentTimeMillis();

        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("F:\basic\untitled13\src\it\cast\day15\demo02\1.jpeg"));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("F:\basic\untitled13\src\it\cast\day15\demo02\3.jpeg"));
        /*
        int len=0;
        while ((len=bis.read())!=-1){
            bos.write(len);
        }//复制文件共耗时:34毫秒*/

        byte[] bytes1=new byte[1024];
        int len=0;
        while ((len=bis.read(bytes1))!=-1){
            //System.out.println(new String(bytes1,0,len));//读取有效的几个
            bos.write(bytes1,0,len);
        }//复制文件共耗时:5毫秒

        bis.close();
        bos.close();
        long e=System.currentTimeMillis();
        System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
    }
}

基本流复制文件:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//文件的复制
public class Demo03Copy {
    public static void main(String[] args) throws IOException {
        long s=System.currentTimeMillis();
        FileInputStream fis=new FileInputStream("F:\basic\untitled13\src\it\cast\day15\demo02\1.jpeg");
        FileOutputStream fos=new FileOutputStream("F:\basic\untitled13\src\it\cast\day15\demo02\2.jpeg");
/*        byte[] bytes1=new byte[1024];
        int len=0;
        while ((len=fis.read(bytes1))!=-1){
            //System.out.println(new String(bytes1,0,len));//读取有效的几个
            fos.write(bytes1,0,len);
        }//复制文件共耗时:10毫秒*/

        int len=0;
        while((len=fis.read())!=-1){
            fos.write(len);

        }//复制文件共耗时:4502毫秒
        fos.close();
        fis.close();
        long e=System.currentTimeMillis();
        System.out.println("复制文件共耗时:"+(e-s)+"毫秒");

    }
}

 复制的文件大小是359 KB (367,976 字节)的jpeg格式的图片,在同一文件夹下面复制文件。

原文地址:https://www.cnblogs.com/cy2268540857/p/13797664.html