Introduction | Heaton Research

Introduction

Java contains an extensive array of classes for file access. A series of readers, writers and filters make up the interface to the physical file system of the computer. The advantage to this sort of system of classes is that the programmer is freed from the burden of dealing with the physical layout of files. The main disadvantage to this architecture is that the programmer is isolated from the physical details of how a file is stored. Java programs have a distinct, and well-defined, way in which they store data to files. Unfortunately, this complicates matters when dealing with files created by other languages.

This article presents a reusable class, that deals with binary files. Methods are provided which allow the programmer to read a variety of standard numeric and string formats. Additional methods are provided which take into account signed/unsigned, little/big-endian storage, as well as file alignment. Using this class, the programmer can read nearly any sort of binary file. An example program is provided that will read the header from a GIF file.

One of the first problems to overcome is reading an unsigned byte. Java treats nearly all types as signed. In order to do the mathematics later required to convert bytes into larger data types, the bytes must be unsigned. A protected method is provided to read bytes in an unsigned form. Converting the byte to a short, and then trimming all but the least significant eight bits does this. This is done with the following lines of code:

protected short readUnsignedByte()
{
    	return (short)(_file.readByte() & 0xff);
}
Copyright 2005-2009 by Heaton Research, Inc.