您的位置:首页 > 编程语言 > Java开发

java8 NIO FileChannel例

2016-06-08 11:15 295 查看
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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: