java8 NIO FileChannel例

来源:互联网 发布:数据新闻比赛官网 编辑:程序博客网 时间:2024/05/20 17:25
package com.kd.nio;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class FileChannelTest {@SuppressWarnings("resource")public static void main(String[] args) throws Exception{FileInputStream fileInputStream = new FileInputStream("f:"+ File.separator +"15000352101265508578.pdf");FileOutputStream fileOutputStream = new FileOutputStream("f:" + File.separator + "test.pdf");   FileChannel inChannel = fileInputStream.getChannel();FileChannel outChannel= fileOutputStream.getChannel();ByteBuffer byteBuffer = ByteBuffer.allocate(1024); int read = inChannel.read(byteBuffer);while(read!=-1){byteBuffer.flip();outChannel.write(byteBuffer);byteBuffer.clear();read = inChannel.read(byteBuffer);}inChannel.close();outChannel.close();}}

0 0
原创粉丝点击