Netty学习笔记 3.6 应用实例4-拷贝文件transferFrom 方法

2022-10-22,,,,

应用实例4-拷贝文件transferFrom 方法

实例要求:

使用 FileChannel(通道) 和 方法 transferFrom ,完成文件的拷贝

拷贝一张图片
代码演示

为方便自我查看及分类
具体代码注释及上文请查看我得上一篇博客Netty学习笔记 3.5 应用实例3-使用一个Buffer完成文件读取

package com.my.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

public class NIOFileChannel04 {
    public static void main(String[] args)  throws Exception {

        //创建相关流
        FileInputStream fileInputStream = new FileInputStream("d:\\a.jpg");
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\a2.jpg");

        //获取各个流对应的filechannel
        FileChannel sourceCh = fileInputStream.getChannel();
        FileChannel destCh = fileOutputStream.getChannel();

        //使用transferForm完成拷贝
        destCh.transferFrom(sourceCh,0,sourceCh.size());
        //关闭相关通道和流
        sourceCh.close();
        destCh.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}

本文地址:https://blog.csdn.net/zyzy123321/article/details/107592827

《Netty学习笔记 3.6 应用实例4-拷贝文件transferFrom 方法.doc》

下载本文的Word格式文档,以方便收藏与打印。