Java Reference
In-Depth Information
put 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
in-
terfaces (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
public
RandomAccessFile(String
name,
String
mode)
throws
FileNotFoundException
Creates a random access file stream to read from, and op-
tionally write to, a file with the specified name. The basic
mode can be either
"r"
or
"rw"
for read or read/write, respect-
ively. 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
IllegalArgumentEx-
ception
. If the mode contains
"rw"
and the file does not ex-
ist, it will be created or, if that fails, a
FileNotFoundException
is
thrown.
public
RandomAccessFile(File
file,
String
mode)
throws
FileNotFoundException