get FileChannel without using java.io.* (use pure NIO)

channel, copy, file-io, java, nio

Solution

The javadoc of FileChannel says:

This class does not define methods for opening existing files or for creating new ones; such methods may be added in a future release. In this release a file channel can be obtained from an existing FileInputStream, FileOutputStream, or RandomAccessFile object by invoking that object's getChannel method, which returns a file channel that is connected to the same underlying file.

That is, with java 1.6 you can't get a `FileChannel` without using old `java.io`.

Problem

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)

Original source

Related problems