这里通过一个将文件src拷贝到文件dst的Demo,来延时FileChannel是如何进行读写的
String srcFile = "C://1.txt";
String dstFile = "C://2.txt";
FileChannel srcFileChannel = FileChannel.open(Paths.get(srcFile), StandardOpenOption.READ);
FileChannel dstFileChannel = FileChannel.open(Paths.get(dstFile), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
while (srcFileChannel.read(byteBuffer) != -1) {
byteBuffer.flip();
while (byteBuffer.position() < byteBuffer.limit())
dstFileChannel.write(byteBuffer);
byteBuffer.clear();
}
dstFileChannel.force(true);
srcFileChannel.close();
dstFileChannel.close();