FileChannel

Create memory mapped file

This is an example of how to create a memory mapped file in Java. Doing reads and writes of data using Java NIO Channels implies that you should :

  • Create a File object to encapsulate an actual file in the file system
  • Create a random access file stream (read only, read-write). To do so you must first create a RandomAccessFile object to encapsulate the file object created above and open it for read only or read-write operations. Then use the getChannel() API method of the RandomAccessFile object to get the file channel to read/write data from/to
  • Map a region of this channel’s file directly into memory by using the map(MapMode,int,int) API method of the FileChannel class. This method returns a handle to a ByteBuffer class for reading/writing data
  • The MapMode.PRIVATE option forces that changes made to the resulting buffer will not be propagated to the file and will not be visible to other programs that have mapped the same file; instead, they will cause private copies of the modified portions of the buffer to be created

as described in the code snippet below.

 
Do not forget to close the channel after you are done processing the file so as to release operating system resources.

package com.javacodegeeks.snippets.core;

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

public class CreateMemoryMappedFile {
	
	public static void main(String[] args) {
		
		try {
			
			File file = new File("myfile.dat");
			
			// create a random access file stream (for read only)
		    FileChannel readOnlyChannel = new RandomAccessFile(file, "r").getChannel();
		    // map a region of this channel's file directly into memory
		    ByteBuffer readOnlyBuf =



    readOnlyChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) readOnlyChannel.size());

		    // create a random access file stream (read-write)
		    FileChannel readWriteChannel = new RandomAccessFile(file, "rw").getChannel();
		    // map a region of this channel's file directly into memory
		    ByteBuffer readWriteBuf =



  readWriteChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) readWriteChannel.size());

		    // create a random access file stream (private/copy-on-write))
		    FileChannel privateChannel = new RandomAccessFile(file, "rw").getChannel();
		    // map a region of this channel's file directly into memory
		    ByteBuffer privateBuf =



    privateChannel.map(FileChannel.MapMode.PRIVATE, 0, (int) privateChannel.size());
		    
		}
		catch (IOException e) {
			System.out.println("I/O Error: " + e.getMessage());
		}
		
	}

}

This was an example of how to create a memory mapped file in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button