We would like to know how to get FileChannel from FileInputStream.
//from w w w .j a va 2 s .co m import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class MainClass { private static final int BSIZE = 1024; public static void main(String[] args) throws Exception { FileChannel fc = new FileInputStream("data.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(BSIZE); fc.read(buff); buff.flip(); while (buff.hasRemaining()) System.out.print((char) buff.get()); } }
The code above generates the following result.