February 28, 2021
Hot Topics:

Introduction to Memory-Mapped IO in Java

  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Java Programming Notes # 1794


New features in SDK Version 1.4.0

The recently released JavaTM 2 SDK, Standard Edition Version 1.4 contains a large number of new features, including the concept of IO channels.  The first lesson in this miniseries, entitled FileChannel Objects in Java, Background Information, introduced you to the concept of channels from a read/write IO viewpoint. 

Mixed primitive types

The previous lesson, entitled FileChannel Objects in Java, Records with Mixed Types, showed you how to use the FileChannel class along with the ByteBuffer class to:

  • Create records consisting of sequences of data values of mixed primitive types
  • Manipulate those records under program control
  • Transfer those records between the computer's memory and a physical disk file using read/write operations on the channel

Memory-mapped IO

In this lesson, I'm will teach you how to use the FileChannel class along with the ByteBuffer class to perform memory-mapped IO for data of type byte.  This is an alternative to the read/write approach discussed in previous lessons.

This lesson will teach you the basics of memory-mapped IO using data of type byte.  In the next lesson, I will teach you how to do memory-mapped IO for data records containing mixed types of data.  I will also teach you how to do memory-mapped IO for different data types using different views of the data in the buffer.

Viewing tip

You may find it useful to open another copy of this lesson in a separate browser window.  That will make it easier for you to scroll back and forth among the different listings and figures while you are reading about them.

Supplementary material

I recommend that you also study the other lessons in my extensive collection of online Java tutorials.  You will find those lessons published at Gamelan.com.  However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there.  You will find a consolidated index at www.DickBaldwin.com.

What is a FileChannel?

Sun describes an object of the FileChannel class simply as "A channel for reading, writing, mapping, and manipulating a file."

Discussion and Sample Code

Mapping files into memory

Previous lessons have explained the use of FileChannel objects for reading and writing files.  In this lesson, I will show you how to use FileChannel objects for mapping files into memory. 

Once you map a file into memory, changes that you make to the memory map are automatically propagated to the file.

Will discuss sample program in fragments

I will illustrate memory-mapped IO for data of type byte using the sample program named Channel04.  As usual, I will discuss this program in fragments.  You will find a complete listing of the program in Listing 17 near the end of the lesson.

Description of the program

This program, which was tested using JDK 1.4.0 under Win2000, illustrates the use of FileChannel objects for memory-mapped IO.

The program first creates and populates a disk file with data of type byte, closes all connections between the program and the file, and makes all references to the data eligible for garbage collection.

Then the program creates a memory map of the existing file, closes the channel used to create the map, and displays the contents of the map.

Then the program modifies the map, which automatically modifies the contents of the associated file.  (Note that there is no open channel connecting the file to the program at this point.)

Then the program reads and displays the modified file.

The above steps illustrate the basic capability of memory-mapped IO.

Create a partial read-only map

To illustrate some additional capabilities of memory-mapped IO, the program then creates a read-only map of a portion of the modified file and displays it.

The main method

For simplicity, this program consists of a main method and a static convenience method used to display the data in a ByteBuffer object.

The first fragment in Listing 1 shows the beginning of the main method.

  public static void main(
                        String[] args){
    byte[] array = {65,66,67,68,69,70};
    ByteBuffer buf = 

Listing 1

The wrap method

In an earlier lesson, you learned that one way to create and populate a ByteBuffer object is to invoke the static wrap method of the ByteBuffer class to wrap the buffer object around an existing array object of type byte[](I will show you a different way to create a ByteBuffer object later in this lesson.)

The code in Listing 1 performs such a wrapping operation to create a new ByteBuffer object, which is populated with six sequential byte values from 65 through 70 inclusive.  This will be the starting point for the exercises that follow.

Display the data in the buffer

The code in Listing 2 invokes the showBufferData method to display the contents of the buffer.

    showBufferData(buf,"buf-raw data");

Listing 2

The showBufferData method was explained in an earlier lesson, and won't be discussed further here.  You can view the entire method in Listing 17 near the end of the lesson.

The output

The output produced by Listing 2 is shown in Figure 1.

Data for buf-raw data
65 66 67 68 69 70

Figure 1

This output simply confirms that the contents of the ByteBuffer object are the same as the contents of the original array object.

(You also learned in an earlier lesson that changes made to the array object will be reflected in the buffer and vice versa.  In other words, I can't tell you how this process is handled in physical memory, but the effect is as if the array object and the buffer object contain the same data.)

Page 1 of 4

This article was originally published on November 26, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date