四种字节流复制文件速度对比

这篇博客对比了四种Java字节流复制文件的方法,包括:字节流逐个读取输出、字节流结合数组读取输出、字节缓冲流逐个读取输出以及字节缓冲流结合数组读取输出。实验结果显示,使用字节缓冲流配合数组的复制方式速度最快,其次是使用字节缓冲流逐个读取。测试文件为234.4 MB的Navicat.zip。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用字节流逐个读取并输出

2、使用字节流+数组进行读取输出

3、使用字节缓冲流逐个读取输出

4、使用字节缓冲流+数组读取输出

实验所用文件为Navicat.zip 大小为234.4 MB

package java_20220218.work.work_2;

import java.io.*;

/**
 * @ClassName Work_2
 * @Description 编写出你认为文件复制操作性能较高的案例。
 * @Author chenxys
 * @Date 2022/2/18 17:48
 * @Version
 */
public class Work_2 {
    static String path = "/Users/chenys/Desktop/test/Navicat.zip";
    static String pathCopy = "/Users/chenys/Desktop/test/Navicat";
    static long startTime;//存放开始时间
    static long endTime;//存放结束时间
    static int res = 0;//存放读取到到数据

    public static void main(String[] args) throws IOException {
        //字符流不能用于视频的读取和复制
        fileInputStream_1("1.zip");
        fileInputStream_2("2.zip");
        fileInputStream_3("3.zip");
        fileInputStream_4("4.zip");
    }
    //对比
    //1、使用字节流逐个读取
    public static void fileInputStream_1(String pp) throws IOException {
        //创建字节流对象
        InputStream in = new FileInputStream(path);
        OutputStream out = new FileOutputStream(pathCopy+pp);//拼接目标文件
        //1、逐个读取
        startTime = System.currentTimeMillis();//将开始时间设为当前时间
        while ((res=in.read()) != -1 ){//对文件进行读取,如果读完则返回-1
           out.write(res);//将读取到到写入到目标文件
        }
        endTime = System.currentTimeMillis();//结束时间
        //关闭源
        in.close();
        out.close();
        System.out.println("使用字节流逐个读取花费时间:"+(endTime-startTime)+"毫秒");
    }
    //2、利用字节数组读取
    public static void fileInputStream_2(String pp) throws IOException {
        //创建字节流对象
        InputStream in = new FileInputStream(path);
        OutputStream out = new FileOutputStream(pathCopy+pp);//拼接目标文件
        byte[] buff = new byte[1024];//声明字节数组
        startTime = System.currentTimeMillis();//将开始时间设为当前时间
        while ((res=in.read(buff)) != -1 ){//将读取到到数据存入数组
            out.write(buff,0,res);//将数组中到数据写入目标文件
        }
        endTime = System.currentTimeMillis();//结束时间
        //关闭源
        in.close();
        out.close();
        System.out.println("使用字节流数组读取花费时间:"+(endTime-startTime)+"毫秒");
    }

    //3、利用缓冲字节流数组读取
    public static void fileInputStream_3(String pp) throws IOException {
        //创建字节流对象
        FileInputStream in = new FileInputStream(path);
        FileOutputStream out = new FileOutputStream(pathCopy+pp);//拼接目标文件
        //创建缓冲字节流对象
        BufferedInputStream inBuff = new BufferedInputStream(in);
        BufferedOutputStream outBuff = new BufferedOutputStream(out);
        byte[] buff = new byte[1024];//声明字节数组
        startTime = System.currentTimeMillis();//开始时间为当前系统时间
        while ((res=inBuff.read(buff)) != -1 ){//将读取到到数据存入字节数组
            outBuff.write(buff,0,res);//写入目标文件
        }
        outBuff.flush();
        endTime = System.currentTimeMillis();//结束时间
        //关闭源
        in.close();
        out.close();
        inBuff.close();
        outBuff.close();
        System.out.println("使用字节缓冲流数组读取花费时间:"+(endTime-startTime)+"毫秒");
    }

    //4、利用缓冲字节流逐个读取
    public static void fileInputStream_4(String pp) throws IOException {
        //创建字节流对象
        FileInputStream in = new FileInputStream(path);
        FileOutputStream out = new FileOutputStream(pathCopy+pp);//拼接目标文件
        //创建字节缓冲流对象
        BufferedInputStream inBuff = new BufferedInputStream(in);
        BufferedOutputStream outBuff = new BufferedOutputStream(out);
        //逐个读取
        startTime = System.currentTimeMillis();//开始时间:当前系统时间
        while ((res=inBuff.read()) != -1 ){//逐个读取
            outBuff.write(res);//写入目标文件
        }
        outBuff.flush();//刷新至持久层
        endTime = System.currentTimeMillis();//结束时间
        //关闭源
        in.close();
        out.close();
        inBuff.close();
        outBuff.close();
        System.out.println("使用字节缓冲流逐个读取花费时间:"+(endTime-startTime)+"毫秒");
    }
}
1、使用字节流逐个读取花费时间:632000毫秒
2、使用字节流数组读取花费时间:870毫秒
3、使用字节缓冲流数组读取花费时间:236毫秒
4、使用字节缓冲流逐个读取花费时间:2556毫秒

结果显而易见:

        1、使用字节缓冲流+数组复制文件比使用字节流+数组快

        2、使用字节缓冲流逐个读取比使用字节流逐个读取快

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值