Save changes to memory mapped ByteBuffer from FileChannel

In this example we shall show you how to write data to a file using a memory mapped ByteBuffer and a NIO FileChannel 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-write). To do so you must first create a RandomAccessFile object to encapsulate the file object created above and open it for 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 MappedByteBuffer class for reading/writing data
  • Write data into this buffer at the current position using the put(byte) API method of the MappedByteBuffer class. Do not forget to force() any changes made to this buffer’s contents so as to be written to the storage device containing the mapped file

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.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class SaveChangesToMemoryMappedByteBufferFromFileChannel {
	public static void main(String[] args) {
		try {
			File file = new File("myfile.dat");
			// create a random access file stream (read-write)
		    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
		    // map a region of this channel's file directly into memory
		    MappedByteBuffer buf =, 0, (int) channel.size());

		    // write the given byte into this buffer at the current
		    // position, and then increment the position

		    // force any changes made to this buffer's content to be written
		    // to the storage device containing the mapped file

		    // close the channel
		catch (IOException e) {
			System.out.println("I/O Error: " + e.getMessage());


This was an example of how to write data to a file using a memory mapped ByteBuffer and a NIO FileChannel in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

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

I have read and agree to the terms & conditions


Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

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

Inline Feedbacks
View all comments
Back to top button