美文网首页
NIO示例2

NIO示例2

作者: 谁在烽烟彼岸 | 来源:发表于2018-03-27 11:39 被阅读0次

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

相关文章

网友评论

      本文标题:NIO示例2

      本文链接:https://www.haomeiwen.com/subject/lsmjcftx.html