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 aFile 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 aRandomAccessFile 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 theFileChannel class. This method returns a handle to aByteBuffer class for reading/writing data - TheMapMode.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.
Want to be a Java NIO Master ?
Subscribe to our newsletter and download the JDBCUltimateGuideright now!
In order to help you master Java NIO Library, we have compiled a kick-ass guide with all the major Java NIO features and use cases! Besides studying them online you may download the eBook in PDF format!

Thank you!
We will contact you soon.
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!
We will contact you soon.
Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.



