1. 文件拷贝主要涉及对象
- Channel: 相当于传统IO中流
- Buffer
2. 具体实现代码
public class NioFileCopy {
public static void main(String[] args) throws Exception {
String src = "d:\\file.pdf";
String des = "d:\\file1.pdf";
FileInputStream fileInputStream = new FileInputStream(src);
FileChannel inChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream(des);
FileChannel outChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(512);
while(inChannel.read(byteBuffer) != -1) {
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.flip();
}
}
}