O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.8. Read/Write Operations with a Single File Channel

If you want to be able to read from and write to a file using a single channel, you must use the channel provided by a RandomAccessFile object. A RandomAccessFile object is not related to the other file stream classes since its only base class is Object. Its original purpose was to provide random access to a file, which the other file stream classes could not, but as you have seen, this capability has been usurped by a channel anyway.

Two constructors are available for creating RandomAccessFile objects, and both require two arguments. For one constructor, the first argument is a File object that identifies the file path, and the second is a String object that specifies the access mode. The other constructor offers the alternative of using a String object as the first argument specifying the file path, with the second argument defining the access mode as before.

The access mode can be any of the following four values:

"r"Indicates that you just want to read the file. In this mode the file cannot be written.
"rw"Indicates that you want to open the file to allow both read and write operations.
"rwd"Indicates that you want to allow both read and write operations but you want all write operations to force immediate writing of data to the device.
"rws"Indicates that you want to allow both read and write operations but you want all write operations to force immediate writing of data and metadata (such as the file length) to the device. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required