ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

基于java nio的channel实现高效率复制文件

2021-07-12 17:01:32  阅读:173  来源: 互联网

标签:java nio srcFile end start File new null channel


基于java nio的channel实现高效率复制文件
关键:其中的transferFrom基于零拷贝技术

package com.netty.redis.zookeeper.netty.unit1;

import lombok.extern.slf4j.Slf4j;

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

/**
 * @Description: 第一天练习
 * 通过使用 Channel 通道,完成复制文件。
 * 本环节的目标是掌握以下知识:Java NIO 中 ByteBuffer、Channel 两个重要组件的使用。
 * 接着是升级实战的案例,使用文件 Channel 通道的 transferFrom 方法,完成高效率的文件复制。
 * @ClassName: oneday
 * @Author zhaomx
 * @Version 1.0
 * @Date 2021-07-12 16:12
 */
@Slf4j
public class oneday {

    public static void main(String[] args) throws IOException {
        File srcFile = new File("E:\\cnki\\zhaomx-study\\git\\netty\\jdk-master.zip");
        File dstFile = new File("E:\\cnki\\zhaomx-study\\git\\netty\\jdk-master-copy.zip");
        copyFileByNio(srcFile, dstFile);

        File dstFileIo = new File("E:\\cnki\\zhaomx-study\\git\\netty\\jdk-master-copy1.zip");
        copyFileByIo(srcFile, dstFileIo);
    }

    /**
     * 基于nio的Channel连接管道复制,transferFrom方法,零拷贝技术,减少cpu与缓存的io次数
     *
     * @param srcFile
     * @param dstFile
     * @throws IOException
     */
    public static void copyFileByNio(File srcFile, File dstFile) throws IOException {
        long start = System.currentTimeMillis();
        log.info("nio channel start time {}", start);
        if (srcFile == null || !srcFile.exists()) {
            return;
        }

        FileInputStream fileIns = null;
        FileOutputStream fileOuts = null;

        FileChannel source = null;
        FileChannel destination = null;

        try {
            fileIns = new FileInputStream(srcFile);
            fileOuts = new FileOutputStream(dstFile);
            source = fileIns.getChannel();
            destination = fileOuts.getChannel();
            destination.transferFrom(source, 0, source.size());
            long end = System.currentTimeMillis();
            log.info("end time {}", end);
            log.info("nio channel start-end time {}", end - start);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileIns != null)
                fileIns.close();
            if (fileOuts != null)
                fileOuts.close();
            if (source != null)
                source.close();
            if (destination != null)
                destination.close();
        }
    }

    /**
     * 传统io基于流复制
     * @param srcFile
     * @param dstFile
     */
    public static void copyFileByIo(File srcFile, File dstFile) {
        long start = System.currentTimeMillis();
        log.info("io write start time {}", start);
        try {
            InputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile));
            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(dstFile));
            byte[] bytes = new byte[1024];
            int i;
            // 读取到输入流数据,然后写入到输出流中去,实现复制
            while ((i = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, i);
            }
            long end = System.currentTimeMillis();
            log.info("end time {}", end);
            log.info("io write start-end time {}", end - start);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

运行结果:
运行结果

标签:java,nio,srcFile,end,start,File,new,null,channel
来源: https://blog.csdn.net/star_pluss/article/details/118678837

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有