5种方法实现文件的拷贝(IO流)及比较效率

5种方法实现文件的拷贝(IO流)及比较效率

复习了一下IO流,简单的写了下几种文件拷贝,不是很严谨的比较了下他们之间的效率

package io_stream;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.nio.channels.FileChannel;

/**
 * @author Huakai
 * @version 1.0
 * @create 通过复制文件来对比字节流使用缓冲流的效率对比
 * @since 2021/5/8 15:05
 */
public class TestCopy_03 {


    // 简单使用字节流 且不使用缓冲数组
    public void copy(File src, File des) throws IOException {
        FileInputStream inputStream = new FileInputStream(src);
        FileOutputStream outputStream = new FileOutputStream(des);
        int len;
        while ((len = inputStream.read()) != -1) {
            outputStream.write(len);
        }
        outputStream.close();
        // outputStream.flush();
        inputStream.close();
    }

    // 简单使用字节流 且使用缓冲数组
    public void copyOfArray(File src, File des) throws IOException {
        FileInputStream inputStream = new FileInputStream(src);
        FileOutputStream outputStream = new FileOutputStream(des);
        int len;
        byte[] bytes = new byte[1024];
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        outputStream.close();
        inputStream.close();
    }

    // 简单使用处理流 且使用不缓冲数组
    public void bufferedCopy(File src, File des) throws IOException {
        FileInputStream inputStream = new FileInputStream(src);
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(des));
        int len;
        while ((len = inputStream.read()) != -1) {
            outputStream.write(len);
        }
        outputStream.close();
        inputStream.close();
    }

    // 简单使用处理流 且使用缓冲数组
    public void bufferedCopyOfArray(File src, File des) throws IOException {
        FileInputStream inputStream = new FileInputStream(src);
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(des));
        int len;
        byte[] bytes = new byte[1024];
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        outputStream.close();
        inputStream.close();
    }

    /**
     * FileChannel 文件通道拷贝
     * jdk 1.7以前的写法
     */
    public void channelCopy(File src, File des) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel fisChannel = null;
        FileChannel fosChannel = null;
        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(des);
            fisChannel = fis.getChannel();
            fosChannel = fos.getChannel();
            fisChannel.transferTo(0, fisChannel.size(), fosChannel);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fosChannel.close();
                fisChannel.close();
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * FileChannel 文件通道拷贝
     * jdk 1.8的的写法
     */
    public void channelCopy1(File src, File des) {
        try (FileInputStream fis = new FileInputStream(src);
             FileOutputStream fos = new FileOutputStream(des);
             FileChannel fisChannel = fis.getChannel();
             FileChannel fosChannel = fos.getChannel();) {
            fisChannel.transferTo(0, fisChannel.size(), fosChannel);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 比较不同方法的效率
     */
    @Test
    void test01() throws IOException {
        String dir = "C:\\users\\huakai\\Desktop\\";
        String srcName = "test";
        String desName = "copy";
        String suffix = ".7z";
        File src = new File(dir, srcName + suffix);
        File des = new File(dir, desName + suffix);
        long start_time1 = System.currentTimeMillis();
        copy(src, des);
        long end_time1 = System.currentTimeMillis();

        long start_time2 = System.currentTimeMillis();
        bufferedCopy(src, des);
        long end_time2 = System.currentTimeMillis();

        long start_time3 = System.currentTimeMillis();
        copyOfArray(src, des);
        long end_time3 = System.currentTimeMillis();

        long start_time4 = System.currentTimeMillis();
        bufferedCopyOfArray(src, des);
        long end_time4 = System.currentTimeMillis();

        long start_time5 = System.currentTimeMillis();
        channelCopy1(src, des);
        long end_time5 = System.currentTimeMillis();

        System.out.println("普通复制花费的时间是" + (end_time1 - start_time1) + "ms");
        System.out.println("使用缓冲流复制花费的时间是" + (end_time2 - start_time2) + "ms");
        System.out.println("普通复制使用缓冲数组花费的时间是" + (end_time3 - start_time3) + "ms");
        System.out.println("使用缓冲流使用缓冲数组复制花费的时间是" + (end_time4 - start_time4) + "ms");
        System.out.println("使用文件通道用复制花费的时间是" + (end_time5 - start_time5) + "ms");
    }
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值