Get MappedByteBuffer from FileChannel : MappedByteBuffer « File « Java Tutorial






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

public class MainClass {
  public static void main(String[] argv) throws Exception {
    RandomAccessFile raf = new RandomAccessFile("test.txt", "r");
    FileChannel fc = raf.getChannel();
    MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

    buffer.clear();
    buffer.flip();

    System.out.println("hasArray=" + buffer.hasArray());
    System.out.println(buffer.toString());

    System.out.flush();
  }
}








11.49.MappedByteBuffer
11.49.1.Create MappedByteBuffer from FileInputStream
11.49.2.Get MappedByteBuffer from FileChannel
11.49.3.What happens when the entire file isn't in your mapping region?
11.49.4.Create Read only buffer
11.49.5.Locking portions of a mapped file
11.49.6.Creating a very large file using mapping
11.49.7.Read file upside/down with RandomAccessFile
11.49.8.Mapping an entire file into memory for reading