|
|
|
|||
|
Using Random Access FilesTheRandomAccessFile class implements both the DataInput and DataOutput
interfaces and therefore can be used for both reading and writing.
RandomAccessFile is similar to FileInputStream and FileOutputStream
in that you specify a file on the native file system to open when you
create it. You can do this with a filename or a
FileRandomAccessFile, you must indicate
whether you will be just reading the file or also writing to it. (You have to
be able to read a file in order to write it.) The
following line of Java code creates a RandomAccessFile to read the file named
farrago.txt:
And this one opens the same file for both reading and writing:
After the file has been opened, you can use the common readXXX
or writeXXX methods to perform I/O on the file.
![]() RandomAccessFile contains three methods
for explicitly manipulating the file pointer.
| |||
|