集册 Java实例教程 持续更改内存

持续更改内存

欢马劈雪     最近更新时间:2020-01-02 10:19:05

376
持久更改到内存映射的ByteBuffer
/**
 * N o w J a v a . c o m - 时代Java 提 供 
**/

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;


public class Main {

  public static void main(String[] argv) throws Exception {

    try {

      // Create a ByteBuffer on a memory-mapped file

      File file = new File("filename");

      FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

      MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0,

          (int) channel.size());


      // Make a change to the ByteBuffer

      buf.put(0, (byte) 0xFF);
      /*
      时代Java公众号 - nowjava.com 提 供
      */


     
展开阅读全文