RandomAccessFile encapsulates a random-access file.
It is not derived from InputStream or OutputStream.
Instead, it implements the interfaces DataInput and DataOutput, which define the basic I/O methods.
It implements the Closeable interface.
RandomAccessFile supports positioning requests, you can position the file pointer within the file.
It has these two constructors:
RandomAccessFile(File fileObj, String access) throws FileNotFoundException
fileObj specifies the name of the file to open as a File object.
RandomAccessFile(String filename, String access) throws FileNotFoundException
the name of the file is passed in filename.
access parameter determines what type of file access is permitted.
access Meaning
r the file can be read, but not written.
rw opened in read-write mode.
rws opened for read-write operations and every change (data and metadata) will be immediately written to the physical device.
rwd opened for read-write operations and every change to the file's data will be immediately written to the physical device.
The method seek( ) sets the current position of the file pointer within the file:
void seek(long newPos) throws IOException
newPos specifies the new position, in bytes, of the file pointer from the beginning of the file. After a call to seek( ), the next read or write operation will occur at the new file position.
It includes some additional methods.
One is setLength( ). It has this signature:
void setLength(long len) throws IOException
This method sets the length of the invoking file to that specified by len. This method can be used to lengthen or shorten a file. If the file is lengthened, the added portion is undefined.