Home » Core Java » nio » FileChannel » Create memory mapped file

About Ilias Tsagklis

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

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.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of