Java FileChannel.read(ByteBuffer dst, long position)

Syntax

FileChannel.read(ByteBuffer dst, long position) has the following syntax.

public abstract int read(ByteBuffer dst, long position)   throws IOException

Example

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  .  j a va2  s. co m*/
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();
  }
}




















Home »
  Java Tutorial »
    java.nio.channels »




FileChannel