Persisting Changes to a Memory-Mapped ByteBuffer : MappedByteBuffer « File Input Output « Java






Persisting Changes to a Memory-Mapped ByteBuffer

 

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

public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    buf.force();

    channel.close();
  }
}

   
  








Related examples in the same category

1.Read file upside/down with RandomAccessFile
2.Map FileChannel to MappedByteBuffer
3.Use a mapped file to read a text file.