java(IO流对象)四种文件复制方式的效率比较

四种文件复制方式的效率比较

  • 文件复制方式,字节流,一共4个方式,源文件的大小是:40.8 MB (42,885,472 字节)
  • 1.字节流读写单个字节 运行时间:210546ms
  • 2.字节流读写字节数组 运行时间:438ms
  • 3.字节流缓冲区读写单个字节 运行时间:2304ms
  • 4.字节流缓冲区读写字节数组 运行时间:137ms
public static void main(String[] args) {
        long start=System.currentTimeMillis();
        File src = new File("E:\\software\\kugou8322.exe");
        File desc = new File("d:\\oracle code\\longzhoufeng\\kugou.exe");
        copy_1(src, desc);
        long end=System.currentTimeMillis();
        System.out.println(end-start);
    }

方法4:实现文件复制

 /**
     * 方法4:实现文件复制
     * 1.字节流缓冲区读写字节数组
     * 运行时间:137ms
     */
    public static void copy_4(File src, File desc) {
        try {
            BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(src));
            BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(desc));
            int len = 0;
            byte[] bt=new byte[1024];
            while ((len = bufis.read(bt)) != -1) {
                bufos.write(bt,0,len);
            }
            bufis.close();
            bufos.close();
        } catch (IOException ex) {
            System.out.println(ex);
            throw new RuntimeException("文件写入失败");
        }
    }

方法3:实现文件复制

 /**
     * 方法3:实现文件复制
     * 1.字节流缓冲区读写单个字节
     * 运行时间:2304ms
     */
    public static void copy_3(File src, File desc) {
        try {
            BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(src));
            BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(desc));
            int len = 0;
            while ((len = bufis.read()) != -1) {
                bufos.write(len);
            }
            bufis.close();
            bufos.close();
        } catch (IOException ex) {
            System.out.println(ex);
            throw new RuntimeException("文件写入失败");
        }
    }

方法2:实现文件复制

/**
     * 方法2:实现文件复制
     * 1.字节流读写字节数组
     * 运行时间:438ms
     */
    public static void copy_2(File src, File desc) {
        try {
            FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos = new FileOutputStream(desc);
            int len = 0;
            byte[] bt = new byte[1024];
            while ((len = fis.read(bt)) != -1) {
                fos.write(bt,0,len);
            }
            fis.close();
            fos.close();
        } catch (IOException ex) {
            System.out.println(ex);
            throw new RuntimeException("文件写入失败");
        }
    }

方法1:实现文件复制

    /**
     * 方法1:实现文件复制
     * 1.字节流读写单个字节
     * 运行时间:210546ms
     */
    public static void copy_1(File src, File desc) {
        try {
            FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos = new FileOutputStream(desc);
            int len = 0;
            while ((len = fis.read()) != -1) {
                fos.write(len);
            }
            fis.close();
            fos.close();
        } catch (IOException ex) {
            System.out.println(ex);
            throw new RuntimeException("文件写入失败");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值