Java IO Tutorial - Java FileChannel .map (FileChannel .MapMode mode, long position, long size)








Syntax

FileChannel.map(FileChannel.MapMode mode, long position, long size) has the following syntax.

public abstract MappedByteBuffer map(FileChannel.MapMode mode,   long position,   long size)    throws IOException

Example

In the following code shows how to use FileChannel.map(FileChannel.MapMode mode, long position, long size) method.

/*w  ww .  j ava 2  s. c  o  m*/
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

public class Main {
  public static void main(String[] args) throws IOException {
    FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
    ib.put(0);
    for (int i = 1; i < 10; i++)
      ib.put(ib.get(i - 1));
    fc.close();

  }
}

The code above generates the following result.