集册 Java实例教程 创建一个私人(副本

创建一个私人(副本

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

446
创建一个专用的(写时复制)内存映射文件。

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;/**nowjava.com - 时  代  Java**/

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;


public class Main {


  public void main(String[] argv) {

    try {

      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();

   
展开阅读全文