最近,我收到了对此答案java.io
的评论,如果我想使用“纯 NIO” ,我应该远离。
这是简化的代码(复制文件):
private static void copy(File source, File destination) throws IOException {
long length = source.length();
FileChannel input = new FileInputStream(source).getChannel();
FileChannel output = new FileOutputStream(destination).getChannel();
input.transferTo(0, length, output);
output.close();
input.close();
}
(代码极其简化:删除了 try-finally 和循环)
我的问题是如何在FileChannel
不使用 java.io ( ) 的情况下获取一个或其他 NIO 类来读取文件FileInputStream
?
编辑:
Java 6(或仅之前)