NIO/BIO本地文件copy对比

本文探讨了四种文件复制方法:nioCopy使用BIO、nio_mapCopy采用映射缓冲区、nio_transCopy进行通道转换,以及bioCopy的传统BIO方式。通过实验对比了它们的耗时,展示了nio技术在文件复制中的优势。

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

测试文件大小:5G
//耗时:147801139000
copyUseBIO(copyFile, file);
//耗时:160264982300
nioCopy(copyFile, file);
//耗时:155988801199
nio_mapCopy(copyFile, file);
//耗时:133744337900
nio_transCopy(copyFile, file);

    private static void nio_transCopy(String copyFile, File file) throws IOException {
        File nioFile = new File(copyFile + "nio_trans_" + file.getName());
        if (!nioFile.exists()){
            nioFile.createNewFile();
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel fileChannel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream(nioFile);
        FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
        long start = System.nanoTime();
        Long limit =Long.valueOf(Integer.MAX_VALUE);
        long nextPosition = 0;
        while (true){
            fileChannel.transferTo(nextPosition,limit,fileOutputStreamChannel);
            nextPosition = nextPosition + limit + 1L;
            if (nextPosition < fileChannel.size()){
                if (nextPosition + limit > fileChannel.size()){
                    limit = fileChannel.size() - nextPosition;
                }
            }
            if (nextPosition > fileChannel.size()){
                break;
            }
        }
        System.out.println("耗时:" + (System.nanoTime() - start));
        fileOutputStreamChannel.close();
        fileOutputStream.close();
        fileChannel.close();
        fileInputStream.close();
    }

    private static void nio_mapCopy(String copyFile, File file) throws IOException {
        File nioFile = new File(copyFile + "nio_map_" + file.getName());
        if (!nioFile.exists()){
            nioFile.createNewFile();
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel fileChannel = fileInputStream.getChannel();


        FileOutputStream fileOutputStream = new FileOutputStream(nioFile);
        FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();

        int i = 0;
        long start = System.nanoTime();
        Long limit =Long.valueOf(Integer.MAX_VALUE);
        long nextPosition = 0;
        while (true){
            MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, nextPosition, limit);
            fileOutputStreamChannel.write(mappedByteBuffer);
            i++;
            nextPosition = nextPosition + limit + 1L;
            if (nextPosition < fileChannel.size()){
                if (nextPosition + limit > fileChannel.size()){
                    limit = fileChannel.size() - nextPosition;
                }
            }else {
                break;
            }
        }
        System.out.println("copy 结束:" + i);
        System.out.println("耗时:" + (System.nanoTime() - start));
        fileOutputStreamChannel.close();
        fileOutputStream.close();
        fileChannel.close();
        fileInputStream.close();
    }

    private static void nioCopy(String copyFile, File file) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);

        // 利用BIO copy 文件
        File nioFile = new File(copyFile + "nio_" + file.getName());
        if (!nioFile.exists()){
            nioFile.createNewFile();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(nioFile);
        FileChannel fileInputStreamChannel = fileInputStream.getChannel();
        FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();

        ByteBuffer fileByteBuffer = ByteBuffer.allocateDirect(1024);

        long start = System.nanoTime();
        while (fileInputStreamChannel.read(fileByteBuffer) != -1){
            fileByteBuffer.flip();
            fileOutputStreamChannel.write(fileByteBuffer);
            fileByteBuffer.compact();
        }
        fileByteBuffer.flip();
        while (fileByteBuffer.hasRemaining()){
            fileOutputStreamChannel.write(fileByteBuffer);
        }
        fileOutputStreamChannel.close();
        fileOutputStream.close();
        fileInputStreamChannel.close();
        fileInputStream.close();
        System.out.println("耗时:" + (System.nanoTime() - start));
    }

    private static void copyUseBIO(String copyFile, File file) throws IOException {

        FileInputStream fileInputStream = new FileInputStream(file);

        // 利用BIO copy 文件
        File bioFile = new File(copyFile + "bio_" + file.getName());
        if (!bioFile.exists()){
            bioFile.createNewFile();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(bioFile);
        byte[] bioBytes = new byte[1024];
        long start = System.nanoTime();
        while (fileInputStream.read(bioBytes) != -1){
            fileOutputStream.write(bioBytes);
        }
        fileOutputStream.flush();
        fileOutputStream.close();
        fileInputStream.close();
        System.out.println("耗时:" + (System.nanoTime() - start));
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值