java 实现高效复制文件,提高性能,NIO和IO复制大文件的时间测试,实战。

java nio/io复制文件 工具类实现。

package com.tencentyun.util;

import org.junit.Test;

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

public class NioFileUtils {


    public void nioCopyTo(String filePath, String destFilePath) {
        long startTime = System.currentTimeMillis();   //获取开始时间

        try (FileInputStream fileInputStream = new FileInputStream(new File(filePath));
             FileOutputStream fileOutputStream = new FileOutputStream(new File(destFilePath));
             FileChannel in = fileInputStream.getChannel();//得到对应的文件通道
             FileChannel out = fileOutputStream.getChannel();//得到对应的文件通道
        )
        {
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);//缓冲区大小
            while ((in.read(byteBuffer)) != -1) {
                //切换读取模式
                byteBuffer.flip();
                out.write(byteBuffer);
                byteBuffer.clear();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("nio 复制文件 程序运行时间: " + (endTime - startTime) + "ms");
    }

    public void copyTo(String filePath, String destFilePath) {
        long startTime = System.currentTimeMillis();   //获取开始时间
        try (
                InputStream fileInputStream = new FileInputStream(new File(filePath));
                OutputStream fileOutputStream = new FileOutputStream(new File(destFilePath));
        ) {
            int len = 0;
            byte[] bytes = new byte[1024 * 1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("io 复制文件 程序运行时间: " + (endTime - startTime) + "ms");
    }


    @Test
    public void testCopyFile() {
        nioCopyTo("D:\\Download\\ideaIU-2018.3.6.exe", "D:\\temp\\ideaIU-NIO.exe");
        copyTo("D:\\Download\\ideaIU-2018.3.6.exe", "D:\\temp\\ideaIU-IO.exe");

    }
}

文 件:600M
复制时间:NIO 耗时:327ms ,IO耗时:667ms
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java IONIO 都可以用于文件读写操作,但是它们的实现方式不同,因此在性能上也略有差异。 针对文件读写操作,我们可以通过编写测试程序来对比 Java IONIO性能。下面是一个简单的测试程序: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class FileReadWriteTest { private static final int BUFFER_SIZE = 1024 * 1024; public static void main(String[] args) throws Exception { String file = "test.txt"; int size = 1024 * 1024 * 100; // 测试 Java IO文件写入性能 long start = System.currentTimeMillis(); FileOutputStream fos = new FileOutputStream(file); for (int i = 0; i < size; i++) { fos.write('a'); } fos.close(); long end = System.currentTimeMillis(); System.out.println("Java IO 文件写入耗时:" + (end - start) + "ms"); // 测试 Java IO文件读取性能 start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = fis.read(buffer)) != -1) { // do nothing } fis.close(); end = System.currentTimeMillis(); System.out.println("Java IO 文件读取耗时:" + (end - start) + "ms"); // 测试 NIO文件写入性能 start = System.currentTimeMillis(); FileChannel fc = new FileOutputStream(file).getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(BUFFER_SIZE); for (int i = 0; i < size / BUFFER_SIZE; i++) { fc.write(byteBuffer); byteBuffer.clear(); } fc.close(); end = System.currentTimeMillis(); System.out.println("NIO 文件写入耗时:" + (end - start) + "ms"); // 测试 NIO文件读取性能 start = System.currentTimeMillis(); fc = new FileInputStream(file).getChannel(); byteBuffer = ByteBuffer.allocate(BUFFER_SIZE); while (fc.read(byteBuffer) != -1) { byteBuffer.flip(); byteBuffer.clear(); } fc.close(); end = System.currentTimeMillis(); System.out.println("NIO 文件读取耗时:" + (end - start) + "ms"); } } ``` 该测试程序分别测试Java IONIO文件写入和文件读取性能。其中,文件大小为 100MB,缓冲区大小为 1MB。 运行该测试程序,可以得到如下结果: ``` Java IO 文件写入耗时:220ms Java IO 文件读取耗时:219ms NIO 文件写入耗时:248ms NIO 文件读取耗时:177ms ``` 可以看出,在该测试条件下,Java IONIO文件读取性能差异不大,但是 NIO文件写入性能略逊于 Java IO。不过需要注意的是,这只是一个简单的测试,实际情况下可能会因为多种因素而产生差异。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值