NIO和IO复制速度测试(文件大小450M)

1.测试传统的IO FileInputStream FileOutputStream

@Test
    public void testIOIs() throws IOException {
        File f = new File("D:\\2.exe");
        if (f.exists()) {
            f.delete();
        }
        long start = System.currentTimeMillis();
        InputStream is = new FileInputStream("D:\\1.exe");
        OutputStream os = new FileOutputStream(f);
        byte buff[] = new byte[1024];
        while (is.read(buff) != -1) {
            os.write(buff);
        }
        long end = System.currentTimeMillis();
        System.out.println((end - start) + "ms");
    }

结果为:2567ms

 

2.测试 1中加BufferedInputStream BufferedOutputStream

@Test
    public void testIOIsBuff() throws IOException {
        File f = new File("D:\\2.exe");
        if (f.exists()) {
            f.delete();
        }
        long start = System.currentTimeMillis();
        InputStream is = new FileInputStream("D:\\1.exe");
        BufferedInputStream bis = new BufferedInputStream(is);
        OutputStream os = new FileOutputStream(f);
        BufferedOutputStream fos = new BufferedOutputStream(os);
        byte buff[] = new byte[1024];
        while (bis.read(buff) != -1) {
            fos.write(buff);
        }
        long end = System.currentTimeMillis();
        System.out.println((end - start) + "ms");
    }

结果为:833ms

 

3.测试  RandomAccessFile

  @Test
    public void testIO() throws IOException {
        File f = new File("D:\\2.exe");
        if (f.exists()) {
            f.delete();
        }
        long start = System.currentTimeMillis();
        RandomAccessFile fromFile = new RandomAccessFile("D:\\1.exe", "rw");
        RandomAccessFile toFile = new RandomAccessFile("D:\\2.exe", "rw");
        byte buff[] = new byte[1024];
        while (fromFile.read(buff) != -1) {
            toFile.write(buff);
        }
        long end = System.currentTimeMillis();
        System.out.println((end - start) + "ms");
    }

结果为:2634ms

4.测试 NIO 中Channel ByteBuffer

@Test
    public void testNIOBuffer() throws IOException {
        File f = new File("D:\\2.exe");
        if (f.exists()) {
            f.delete();
        }
        long start = System.currentTimeMillis();
        FileInputStream is = new FileInputStream("D:\\1.exe");
        FileChannel fci = is.getChannel();
        FileOutputStream os = new FileOutputStream(f);
        FileChannel fco = os.getChannel();
        ByteBuffer buff = ByteBuffer.allocate(1024 * 1024 * 6);
        while (fci.read(buff) != -1) {
            buff.flip();
            fco.write(buff);
            buff.clear();
        }
        long end = System.currentTimeMillis();
        System.out.println((end - start) + "ms");
    }

结果为:776ms channel buffer 传输速度受到buffer大小的影响

5.测试 FileChannel  transferTo FileInputStream FileOutputStream

 @Test
    public void transferTestIO() throws IOException {
        File f = new File("D:\\2.exe");
        if (f.exists()) {
            f.delete();
        }
        long start = System.currentTimeMillis();
        FileInputStream fromFile = new FileInputStream("D:\\1.exe");
        FileOutputStream toFile = new FileOutputStream(f);
        FileChannel fromChannel = fromFile.getChannel();
        FileChannel toChannel = toFile.getChannel();
        long position = 0;
        long count = fromChannel.size();
        fromChannel.transferTo(position, count, toChannel);
        long end = System.currentTimeMillis();
        System.out.println((end - start) + "ms");
    }

测试结果为:430ms

 

  1. 6.测试 FileChannel  transferTo  RandomAccessFile
 @Test
    public void transferTest() throws IOException {
        File f = new File("D:\\2.exe");
        if (f.exists()) {
            f.delete();
        }
        long start = System.currentTimeMillis();
        RandomAccessFile fromFile = new RandomAccessFile("D:\\1.exe", "rw");
        FileChannel fromChannel = fromFile.getChannel();
        RandomAccessFile toFile = new RandomAccessFile("D:\\2.exe", "rw");
        FileChannel toChannel = toFile.getChannel();
        long position = 0;
        long count = fromChannel.size();
        fromChannel.transferTo(position, count, toChannel);
        long end = System.currentTimeMillis();
        System.out.println((end - start) + "ms");
    }

结果为:398ms

 

7.最后总结一下 可以看到 最后这种管道数据转移方式最快

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值