NIO示例2

xiaoxiao2021-03-01  23

package nio;

import java.io.IOException;

import java.nio.channels.FileChannel;

import java.nio.file.Paths;

import java.nio.file.StandardOpenOption;

/**

* Created by liupengfei on 2018/3/27.

* Description: 单个文件之间的传输

*/

public class NIOFileToFile {

public static void main(String[] args)throws IOException {

//开启通道新方式

        FileChannel inChannel = FileChannel.open(Paths.get("/Users/liupengfei/Downloads/java的23种设计模式-动力节点.pdf"), StandardOpenOption.READ);

        FileChannel ouChannel = FileChannel.open(Paths.get("/Users/liupengfei/Downloads/java的23种设计模式-动力节点2.pdf"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);

        //利用通道之间的传输来复制数据,实际用的是内存映射的方式

        inChannel.transferTo(0, inChannel.size(), ouChannel);

        inChannel.close();

        ouChannel.close();

        System.out.println("game over...");

    }

}

让我们来看下java.nio.channels包中的FileChannel

java class diagrams

NIO示例中的ServerSocketChannel 

java class diagrams
转载请注明原文地址: https://www.6miu.com/read-4049987.html

最新回复(0)