Java Reference
In-Depth Information
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.
Theseconstructorsthrow
FileNotFoundException
when
mode
is
"r"
andthe
fileidentifiedby
pathname
cannotbeopened(itmightnotexistoritmightbeadir-
ectory), or when
mode
is
"rw"
and
pathname
is read-only or a directory.
Thefollowingexampledemonstratesthesecondconstructorbyattemptingtoopenan
existing random access file via the
"r"
mode string:
RandomAccessFile raf = new RandomAccessFile("employee.dat",
"r");
Arandomaccessfileisassociatedwitha
file pointer
,acursorthatidentifiestheloca-
tionofthenextbytetowriteorread.Whenanexistingfileisopened,thefilepointeris
set to its first byte, at offset 0. The file pointer is also set to 0 when the file is created.
Write and read operations start at the file pointer and advance it past the number of
byteswrittenorread.Operationsthatwritepastthecurrentendofthefilecausethefile
to be extended. These operations continue until the file is closed.
RandomAccessFile
declaresawidevarietyofmethods.Ipresentarepresentative
sample of these methods in
Table 8-6
.