Recently I got a comment to this answer that I should stay away from java.io
if I want to use "pure NIO".
This is the simplified code (copy a file):
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();
}
(code extremely simplified: removed try-finally and loop)
My question is how to get a FileChannel
or other NIO class for reading a file without using java.io (FileInputStream
)?
EDIT:
Java 6 (or before only)