THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

the same file using two different FileOutputStream objects at the same time.


The flush method of FileOutputStream and FileWriter guarantees that the buffer is flushed to the
underlying file. It does not guarantee that the data is committed to diskthe underlying file system may do its
own buffering. You can guarantee that the data is committed to disk by invoking the sync method on the
file's FileDescriptor object, which will either force the data to disk or throw a
SyncFailedException if the underlying system cannot fulfill this contract.


20.7.2. RandomAccessFile


The RandomAccessFile class provides a more sophisticated file mechanism than the File streams do. A
random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or
index 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.


RandomAccessFile is not a subclass of InputStream, OutputStream, Reader, or Writer
because it can do both input and output and can work with both characters and bytes. The constructor has a
parameter that declares whether the stream is for input or for both input and output.


RandomAccessFile supports read and write methods of the same names and signatures as the byte
streams. For example, read returns a single byte. RandomAccessFile also implements the DataInput
and DataOutput interfaces (see page 537) and so can be used to read and write data types supported in
those interfaces. Although you don't have to learn a new set of method names and semantics for the same
kinds of tasks you do with the other streams, you cannot use a RandomAccessFile where any of the other
streams are required.


The constructors for RandomAccessFile are


publicRandomAccessFile(String name, String mode)throws
FileNotFoundException

Creates a random access file stream to read from, and optionally write to, a
file with the specified name. The basic mode can be either "r" or "rw" for
read or read/write, respectively. Variants of "rw" mode provide additional
semantics: "rws" mode specifies that on each write the file contents and
metadata (file size, last modification time, etc.) are written synchronously
through to the disk; "rwd" mode specifies that only the file contents are
written synchronously to the disk. Specifying any other mode will get you an
IllegalArgumentException. If the mode contains "rw" and the file
does not exist, it will be created or, if that fails, a
FileNotFoundException is thrown.

publicRandomAccessFile(File file, String mode)throws
FileNotFoundException

Creates a random access file stream to read from, and optionally write to, the
file specified by the File argument. Modes are the same as for the
String-based constructor.

Since accessing a file requires a security check, these constructors could throw a SecurityException if
you do not have permission to access the file in that modesee "Security" on page 677.

Free download pdf