Random AccessFile

Files can be created and/or opened for random access in which a mixture of write and read operations can occur until the file is closed. Java supports this random access by providing the concrete RandomAccessFile class.

RandomAccessFile declares the following constructors:

■ RandomAccessFile(File file, String mode) creates and opens a new file if it does not exist, or opens an existing file. The file is identified by file's pathname and is created and/or opened according to the mode that is specified by mode.

■ RandomAccessFile(String pathname, String mode) creates and opens a new file if it does not exist, or opens an existing file. The file is identified by pathname and is created and/or opened according to the mode that is specified by mode.

boolean equals (Object obj)

int hashCode()

CAUTION: Be careful when specifying a pathname for either constructor. You should strive to specify only pathnames that are valid for all platforms on which the application will run, unless you are creating your application to run on a single platform.

Either constructor's mode argument must be one of "r", "rw", "rws", or "rwd"; otherwise, the constructor throws IllegalArgumentException. These string literals have the following meanings:

■ "r" informs the constructor to open an existing file for reading only. Any attempt to write to the file results in a thrown instance of the IOException class.

■ "rw" informs the constructor to create and open a new file if it does not exist for reading and writing, or open an existing file for reading and writing.

■ "rwd" informs the constructor to create and open a new file if it does not exist for reading and writing, or open an existing file for reading and writing. Furthermore, each update to the file's content must be written synchronously to the underlying storage device.

■ "rws" informs the constructor to create and open a new file if it does not exist for reading and writing, or open an existing file for reading and writing. Furthermore, each update to the file's content or metadata must be written synchronously to the underlying storage device.

NOTE: A file's metadata is data about the file and not actual file contents. Examples of metadata include the file's length and the time the file was last modified.

The "rwd" and "rws" modes ensure than any writes to a file located on a local storage device are written to the device, which guarantees that critical data is not lost when the system crashes. No guarantee is made when the file does not reside on a local device.

NOTE: Operations on a random access file opened in "rwd" or "rws" mode are slower than these same operations on a random access file opened in "rw" mode.

These constructors throw FileNotFoundException (a subclass of IOException) when mode is "r" and the file identified by pathname cannot be opened (it might not exist or might be a directory), or when mode is "rw" and pathname is read-only or a directory.

The following example demonstrates the second constructor by attempting to open an existing random access file via the "r" mode string:

RandomAccessFile raf = new RandomAccessFile("employee.dat", "r");

A random access file is associated with a file pointer, a cursor that identifies the location of the next byte to write or read. When an existing file is opened, the file pointer is set to its first byte, at offset 0. The file pointer is also set to 0 when the file is created.

Write or read operations start at the file pointer and advance it past the number of bytes written or read. Operations that write past the current end of the file cause the file to be extended. These operations continue until the file is closed.

RandomAccessFile declares a wide variety of methods. I present a representative sample of these methods in Table 10-6.

Table 10-6. RandomAccessFile Methods Method Description void close() Close the file and release any associated system resources. Subsequent writes or reads result in IOException. Also, the file cannot be reopened with this RandomAccessFile object. This method throws IOException when an I/O error occurs.

FileDescriptor getFD()

long getFilePointer() long length()

int read()

Return the file's associated file descriptor object. This method throws IOException when an I/O error occurs.

Return the file pointer's current zero-based byte offset into the file. This method throws IOException when an I/O error occurs.

Return the length (measured in bytes) of the file. This method throws IOException when an I/O error occurs.

Read and return (as an int in the range 0 to 255) the next byte from the file, or return -1 when the end of the file is reached. This method blocks if no input is available, and throws IOException when an I/O error occurs.

Read up to b.length bytes of data from the file into byte array b. This method blocks until at least one byte of input is available. It returns the number of bytes read into the array, or returns -1 when the end of the file is reached. It throws NullPointerException when b is null, and IOException when an I/O error occurs.

char readChar() Read and return a character from the file. This method reads two bytes from the file starting at the current file pointer. If the bytes read, in order, are b1 and b2, where 0 <= b1, b2 <= 255, the result is equal to (char) ((b1<<8) |b2). This method blocks until the two bytes are read, the end of the file is detected, or an exception is thrown. It throws EOFException (a subclass of IOException) when the end of the file is reached before reading both bytes, and IOException when an I/O error occurs.

Method

Was this article helpful?

0 0

Post a comment