Creating a very large file using mapping : MappedByteBuffer « File « Java Tutorial






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

public class MainClass {
  static int length = 0x8FFFFFF; // 128 Mb

  public static void main(String[] args) throws Exception {
    MappedByteBuffer out = new RandomAccessFile("test.dat", "rw").getChannel().map(
        FileChannel.MapMode.READ_WRITE, 0, length);
    for (int i = 0; i < length; i++)
      out.put((byte) 'x');
    System.out.println("Finished writing");
    for (int i = length / 2; i < length / 2 + 6; i++)
      System.out.print((char) out.get(i));
  }
}








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