【发布时间】:2011-01-03 11:05:05
【问题描述】:
最近收到answer 的评论,如果我想使用“纯 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(或之前的版本)
【问题讨论】:
标签: java file-io copy nio channel