February 27, 2021
Hot Topics:

FileChannel Objects in Java, Records with Mixed Types

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

Java Programming Notes # 1792


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, ByteBuffer Type, showed you how to use view objects to read and write different primitive types into disk files, where all of the data was of the same type.

In this lesson, I will show 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

Memory-mapped IO

Future lessons will teach you how to do memory-mapped IO using channels.

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

Will discuss sample program in fragments

I will illustrate the FileChannel class using the sample program named Channel03.  As is my normal approach, I will discuss this program in fragments.  You will find a complete listing of the program in Listing 12 near the end of the lesson.

Reading and writing records

This program, which was tested using Java SDK version 1.4.0 under Win2000, illustrates the use of FileChannel objects to write, read, and manipulate records containing data of mixed primitive types.

The program writes and then reads several data records where each record contains a char, a byte, and a float.

Beginning is similar to previous programs

To keep things simple, the program consists of a main method and several static convenience methods.  The beginning of the main method is shown in Listing 1.

This program begins pretty much the same as the program in the previous lesson.

  public static void main(
                        String[] args){
    //Create a ByteBuffer with a 
    // capacity of 56 bytes, and all
    // elements initialized to zero.
    ByteBuffer bBuf = 
        ByteBuffer.wrap(new byte[56]);

    //Declare varables for use later
    FileOutputStream oStr;
    FileInputStream iStr;
    FileChannel fileChan;

      // Set ByteBuffer limit to 
      // capacity

Listing 1

Because of the similarity of the code in Listing 1 to the code in the previous lesson, I won't discuss this fragment on a step-by-step basis.  Rather, I will let the comments in the code speak for themselves, and refer you to the previous lesson if you have questions.

The flow of this program departs from the flow of the program in the previous lesson in Listing 2, so that is where I will begin my detailed discussion

Create and store a record

The code in Listing 2 creates a record consisting of three primitive values and stores the record in the ByteBuffer object.  The types of the three values are char, byte, and float respectively.

      char cData = 'A';
      byte bData = 14;
      float fData = (float)1.0/3;

Listing 2

How does it work?

After declaring and populating three variables of types char, byte, and float, the code in Listing 2 invokes the method named putRecord to create the record and store it in the ByteBuffer object.

The putRecord method

The method named putRecord is probably the most significant thing in this lesson, so I will explain its behavior at this point in the discussion.  Following that explanation, I will return to the flow of control in the main method.

The entire method named putRecord is shown in Listing 3.

  static void putRecord(
                       ByteBuffer bBuf,
                       char cData,
                       byte bData,
                       float fData){
  }//end putRecord

Listing 3

Incoming parameters of putRecord method

The method named putRecord receives four incoming parameters.  The first parameter is a reference to a ByteBuffer object, which is the object into which the record is to be stored.

The following three parameters are the three primitive values that are to make up the record.

The put methods of the ByteBuffer class

The record is created and stored in the buffer by successively invoking the following three methods on the ByteBuffer object, passing one of the three primitive values to each of the three methods.

  • putChar(char value)
  • put(byte value)
  • putFloat(float value)

There are many different put methods

As of version 1.4.0, there are about eighteen different methods in the ByteBuffer class that begin with the word put.  As you might guess, these methods are used to store data in a ByteBuffer object.

The relative put methods

There are two overloaded versions of the following method name:

  • putChar(char value)
  • putChar(int index, char value)

The first of these is the method used in Listing 3 above, which takes a single parameter.  This version can be described as follows:

Relative put method for writing a char value.  Writes two bytes containing the given char value, in the current byte order, into this buffer at the current position, and then increments the position by two. The incoming parameter is the char value to be written.

The absolute put methods

Although I didn't use it in this program, you may also be interested in a description of the other overloaded version of this method.  This version takes two parameters, where one parameter is a char value and the other parameter is an index.

Absolute put method for writing a char value.  Writes two bytes containing the given char value, in the current byte order, into this buffer at the given index.  The first parameter is the index at which the bytes will be written.  The second parameter is the char value to be written.

The put methods for different primitive types

Two overloaded versions, (relative and absolute), of the following seven method names are provided by the ByteBuffer class (additional overloaded versions of the method named put are also provided for use with type byte):

  • put
  • putChar
  • putDouble
  • putFloat
  • putInt
  • putLong
  • putShort

Page 1 of 3

This article was originally published on November 14, 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