最近我对answer发表了评论,如果我想使用“纯粹的NIO”,我应该远离java.io
。
这是简化的代码(复制文件):得到FileChannel而不使用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();
}
(代码非常简单:删除尝试,终于和循环)
我的问题是如何得到FileChannel
或其他用于读取文件而不使用java.io的NIO类(FileInputStream
)?
编辑:
Java 6中(或之前只)