FileChannel.read(ByteBuffer dst, long position) has the following syntax.
public abstract int read(ByteBuffer dst, long position) throws IOException
In the following code shows how to use FileChannel.read(ByteBuffer dst, long position) method.
import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; //from w w w. ja va2 s . com public class Main { public static void main(String args[]) throws Exception { FileInputStream fIn = new FileInputStream("test.txt"); FileChannel fChan = fIn.getChannel(); long fSize = fChan.size(); ByteBuffer mBuf = ByteBuffer.allocate((int) fSize); fChan.read(mBuf,10); mBuf.rewind(); for (int i = 0; i < fSize; i++){ System.out.print((char) mBuf.get()); } fChan.close(); fIn.close(); } }
The code above generates the following result.