I/O字节流

下面代码对比:一次读取一个字节和一次读取一个数组及应用缓冲流耗时对比

    /*
    * 复制大文件速率对比
    * */
    //耗时:很大,一分钟以上
    public static void copy1() throws IOException {
        String path="E:\Java\00_0\0.avi";
        String path1="E:\Java\00_0\01.avi";
        FileInputStream fis=new FileInputStream(path);

        FileOutputStream fos=new FileOutputStream(path1);

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

        fis.close();
        fos.close();
    }

    //耗时:556
    public static void copy2() throws IOException {
        String path="E:\Java\00_0\0.avi";
        String path1="E:\Java\00_0\02.avi";
        FileInputStream fis=new FileInputStream(path);

        FileOutputStream fos=new FileOutputStream(path1);

        byte[] arr=new byte[1024];
        int len;
        while ((len=fis.read(arr))!=-1){
            fos.write(arr,0,len);
        }

        fis.close();
        fos.close();
    }


    //耗时:2541
    public static void copy3() throws IOException {
        String path="E:\Java\00_0\0.avi";
        String path1="E:\Java\00_0\03.avi";
        BufferedInputStream fis=new BufferedInputStream(new FileInputStream(path));

        BufferedOutputStream fos=new BufferedOutputStream(new FileOutputStream(path1));

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

        fis.close();
        fos.close();
    }

    //耗时:171
    public static void copy4() throws IOException {
        String path="E:\Java\00_0\0.avi";
        String path1="E:\Java\00_0\04.avi";
        BufferedInputStream fis=new BufferedInputStream(new FileInputStream(path));

        BufferedOutputStream fos=new BufferedOutputStream(new FileOutputStream(path1));

        byte[] arr=new byte[1024];
        int len;
        while ((len=fis.read(arr))!=-1){
            fos.write(arr,0,len);
        }

        fis.close();
        fos.close();
    }
原文地址:https://www.cnblogs.com/zhuyapeng/p/13819710.html