Lesson 11 | RandomAccessFile methods |
Objective | Describe the function of Java RandomAccessFile methods |
Random Access File Methods (Java)
File input and output streams require you to start reading or writing at the beginning of a file and then read or write the file in order, possibly skipping over some bytes or backing up but more or less moving from start to finish.
Sometimes, however, you need to read parts of a file in a more or less random order, where the data near the beginning of the file is not necessarily read before the data nearer the end. Other times you need to both read and write the same file.
For example, in record-oriented applications like databases, the actual data may be indexed; you would use the index to determine where in the file to find the record you need to read or write.
While you could do this by constantly opening and closing the file and skipping to the point where you needed to read, this is far from efficient. Writes are even worse, since you would need to read and rewrite the entire file, even to change just one byte of data.
Random-access files can be read from or written to or both from a particular byte position in the file.
A single random-access file can be both read and written without first being closed. The position in the file where reads and writes start from is indicated by an integer called the file pointer.
Each read or write advances the file pointer by the number of bytes read or written. Furthermore, the programmer can reposition the file pointer at different bytes in the file without closing the file. In Java, random file access is performed through the java.io.RandomAccessFile class. The RandomAccessFile class is not a subclass of java.io.File:
public class RandomAccessFile extends Object
implements DataInput, DataOutput
Among other differences between File objects and RandomAccessFile objects, the RandomAccessFile constructors actually open the file in question and throw an IOException if it doesn't exist:
public RandomAccessFile(String filename, String mode) throws FileNotFoundException
public RandomAccessFile(File file, String mode) throws IOException
The RandomAccessFile class implements both the DataInput and DataOutput interfaces.
Therefore, reads and writes use mehods exactly like the
methods of the DataInputStream and DataOutputStream classes, such as read(), readFully(), readBoolean(), and writeBoolean(). Finally, there are a few miscellaneous methods.
Java I/O
public final FileDescriptor getFD() throws IOException
public int skipBytes(int n) throws IOException
public native void close() throws IOException
public class RandomAccessFile
public class RandomAccessFile
extends Object
implements DataOutput, DataInput, Closeable
- Instances of the RandomAccessFile class support both reading and writing to a random access file.
- A random access file behaves like a large array of bytes stored in the file system.
- There is a kind of cursor into the implied array, called the file pointer.
- Input operations read bytes starting at the file pointer and advance the file pointer past the bytes read.
- If the random access file is created in read/write mode, then output operations are also available.
- Output operations write bytes starting at the file pointer and advance the file pointer past the bytes written.
- Output operations that write past the current end of the implied array cause the array to be extended.
- The file pointer can be read by the getFilePointer method and set by the seek method.
It is generally true of all the reading routines in this class that if end-of-file is reached before the desired number of bytes has been read, an EOFException is thrown.
If any byte cannot be read for any reason other than end-of-file, an IOException other than EOFException is thrown. In particular, an IOException may be thrown if the stream has been closed.