Java Reference
In-Depth Information
Creates a random access file stream to read from, and option-
ally 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
The "random access" in the name of the class refers to the ability to set
the read/write file pointer to any position in the file and then perform
operations. The additional methods in
RandomAccessFile
to support this
functionality are:
public long
getFilePointer()
throws IOException
Returns the current location of the file pointer (in bytes) from
the beginning of the file.
public void
seek(long pos)
throws IOException
Sets the file pointer to the specified number of bytes from the
beginning of the file. The next byte written or read will be the
pos
th
byte in the file, where the initial byte is the 0
th
. If you
position the file pointer beyond the end of the file and write to
the file, the file will grow.
public int
skipBytes(int count)
tHRows IOException
Attempts to advance the file pointer
count
bytes. Any bytes
skipped over can be read later after
seek
is used to reposition
the file pointer. Returns the actual number of bytes skipped.
This method is guaranteed never to throw an
EOFException
. If
count
is negative, no bytes are skipped.
public long
length()
throws IOException
Returns the file length.