Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Use a RandomAccessFile to build a low-level database

How to extend a RandomAccessFile to support the storage and retrieval of arbitrary record data

  • Print
  • Feedback
As I searched JavaWorld's site for ideas for this month's Step by Step, I came across only a few articles covering low-level file access. Although high-level APIs such as JDBC give us the flexibility and power needed in large enterprise applications, many smaller applications require a more simple and elegant solution.

In this article, we will build an extension to the RandomAccessFile class that allows us to store and retrieve records. This "records file" will be equivalent to a persistent hashtable, allowing keyed objects to be stored and retrieved from file storage.

A primer on files and records

Before we jump headlong into the example, let's start with a basic backgrounder. We'll begin by defining some terms pertaining to files and records, then we'll briefly discuss class java.io.RandomAccessFile and platform-dependency.

Terminology

The following definitions are tuned to our example, rather than to traditional database terminology.



Record -- A collection of related data stored in a file. A record typically has multiple fields, each being a named and typed item of information.

Key -- An identifier for a record. Keys are usually unique.

File -- A sequential collection of data stored in some sort of stable storage such as a hard drive.

Nonsequential file access -- Allows data to be read from arbitrary locations in the file.

File pointer -- A number holding the position of the next byte of data to be read from a file.

Record pointer -- A record pointer is a file pointer that points to the location where a particular record begins.

Index -- A secondary means of accessing records in a file; that is, it maps keys to record pointers.

Heap -- A sequential file of unordered and variable-sized records. A heap requires some external indexing in order to meaningfully access the records.

Persistence -- Refers to storing an object or record for a certain length of time. This length of time is typically longer than the span of one process, so objects are usually persisted in files or databases.



Overview of class java.io.RandomAccessFile
Class RandomAccessFile is Java's way of providing nonsequential access to files. The class allows us to jump to a certain location in the file by using the seek() method. Once the file pointer has been positioned, data can be read from and written to the file using the DataInput and DataOutput interfaces. These interfaces allow us to read and write data in a platform-independent manner. Other handy methods in RandomAccessFile allow us to check and set the length of the file.

Platform-dependent considerations
Modern databases rely on disk drives for storage. Data on a disk drive is stored in blocks, which are distributed across tracks and surfaces. The disk's seek time and rotational delay dictate how data can be most efficiently stored and retrieved. A typical database management system relies closely on the disk's attributes in order to streamline performance. Unfortunately (or fortunately, depending on your interest in low-level file I/O!), these parameters lie far from reach when using a high-level file API such as java.io. Given this fact, our example will disregard the optimizations that knowledge of the disk's parameters could provide.

  • Print
  • Feedback

Resources
  • Download the complete source for this article as a zip file http://www.javaworld.com/jw-01-1999/step/jw-01-step.zip
  • Download the modified BaseRecordsFile.java http://www.javaworld.com/jw-01-1999/step/BaseRecordsFile.java
  • View the API documentation for class java.io.RandomAccessFile http://java.sun.com/products/jdk/1.2/docs/api/java/io/RandomAccessFile.html
  • Download the Java 2 platform (JDK 1.2) http://www.javasoft.com/products/jdk/1.2/
  • Read this primer on I/O streams in Java http://developer.java.sun.com/developer/technicalArticles/iostreams/
  • Read this intro to I/O from Sun's Java Tutorial http://www.javasoft.com/docs/books/tutorial/essential/io/index.html
  • Read all the other Step by Step columns http://www.javaworld.com/topicalindex/jw-ti-step.html