Map FileChannel to MappedByteBuffer : MappedByteBuffer « File Input Output « Java






Map FileChannel to MappedByteBuffer

 

import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
  public static void main(String args[]) throws Exception {
    String filename = args[0];
    int start = 10;
    int length = 20;

    FileInputStream fin = new FileInputStream(filename);
    FileChannel finc = fin.getChannel();
    MappedByteBuffer mbb = finc.map(FileChannel.MapMode.READ_ONLY, start, length);

    for (int i = 0; i < length; ++i) {
      System.out.println(mbb.get(i);
    }
    fin.close();
  }
}

   
  








Related examples in the same category

1.Persisting Changes to a Memory-Mapped ByteBuffer
2.Read file upside/down with RandomAccessFile
3.Use a mapped file to read a text file.