Random Access Files

All the file input and output streams discussed so far in this chapter are sequential. This is fine for batch processing, where the entire contents of a file are either read, written to, or updated. However, this sequential mode can be very inefficient when we want to access just a few records in a large file stored on disk. If the record is located at the beginning of the file, there will be little degradation in performance; if the record is located at the end of the file, we will need to read through the entire file to reach our desired record. Random access provides nonsequential or direct access to the contents of a file. In Java, this facility is achieved by maintaining an index or file pointer. The file pointer is moved whenever data is read from or written to the file and so provides the current position in the file.

Java provides the java.io.RandomAccessFile class both for reading from and writing to random access files. The class provides a number of methods, many of which are similar to the read and write methods we have seen for other streams. However, a few methods are unique to this class; the most significant are getFilePointer and seek. getFilePointer returns the current position of the file pointer; seek positions the file pointer just before the specified byte offset.

This means that to randomly access a record, we need to know its position in the file. Consequently, although variable length records are permitted in Java random access files, they are most likely to be of fixed length. Also in practice, we would need an algorithm that would map a record's unique key identifier to a byte offset position in the file.

No comments:

Post a Comment