最近我在这个答案中收到了一条评论,说如果我想使用“纯NIO”,就应该避免使用java.io
。
以下是简化的代码(复制文件):
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和循环)
我的问题是如何在不使用java.io (FileInputStream
)的情况下获取FileChannel
或其他NIO类来读取文件?
编辑:
Java 6 (或之前版本)