Create a private (copy-on-write) memory-mapped file. - Java File Path IO

Java examples for File Path IO:MappedByteBuffer

Description

Create a private (copy-on-write) memory-mapped file.

Demo Code

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {

  public void main(String[] argv) {
    try {/*from w w w  .j  av a2  s .  co  m*/
      File file = new File("filename");
      // Create a read-only memory-mapped file
      FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
      FileChannel rwChannel = new RandomAccessFile(file, "rw").getChannel();
      ByteBuffer roBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0,
          (int) roChannel.size());
      // Create a private (copy-on-write) memory-mapped file.
      // Any write to this channel results in a private copy of the data.
      FileChannel pvChannel = new RandomAccessFile(file, "rw").getChannel();
      ByteBuffer pvBuf = roChannel.map(FileChannel.MapMode.READ_WRITE, 0,
          (int) rwChannel.size());


    } catch (IOException e) {
    }
  }
}

Related Tutorials