使用通道之间的数据传输(效果,也是实现文件的复制)

package com.nio;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import static java.nio.channels.FileChannel.open;

/**
 * 通道之间的数据传输,这种使用的也是直接缓存区的方式
 */
public class TestChannel3 {
    public static void main(String[] args) throws IOException {
        //通道之间的数据传输(直接缓存区)
        FileChannel inChannel = FileChannel.open(Paths.get("001.jpg"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("002.jpg"), StandardOpenOption.WRITE,StandardOpenOption.READ, StandardOpenOption.CREATE);
        //下面的这两种方式效果是一样的 transferTo transferFrom
//        inChannel.transferTo(0,inChannel.size(),outChannel);
        outChannel.transferFrom(inChannel,0,inChannel.size());
        inChannel.close();
        outChannel.close();

    }
}

  

原文地址:https://www.cnblogs.com/dongyaotou/p/14414205.html