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();
}
}
java8 NIO FileChannel例
最新推荐文章于 2023-04-11 16:47:33 发布