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.
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.
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.
java.io.RandomAccessFile http://java.sun.com/products/jdk/1.2/docs/api/java/io/RandomAccessFile.html
Impressive!By Anonymous on August 5, 2009, 10:20 amPerfect workarround for ZipOutputStream which cannot make random access to file...
Reply | Read entire comment
niceBy Anonymous on February 8, 2009, 4:17 amnice
Reply | Read entire comment
View all comments