Creates a random access file stream to read from, and optionally to
write to, the file specified by the
File
argument. A new
FileDescriptor
object is created to represent this file connection.
The mode argument specifies the access mode
in which the file is to be opened. The permitted values and their
meanings are:
Value | Meaning |
"r" |
Open for reading only. Invoking any of the write
methods of the resulting object will cause an IOException to be thrown. |
"rw" |
Open for reading and writing. If the file does not already
exist then an attempt will be made to create it. |
"rws" |
Open for reading and writing, as with "rw", and also
require that every update to the file's content or metadata be
written synchronously to the underlying storage device. |
"rwd" |
Open for reading and writing, as with "rw", and also
require that every update to the file's content be written
synchronously to the underlying storage device. |
The "rws" and "rwd" modes work much like the force(boolean)
method of
the
FileChannel
class, passing arguments of
true and
false, respectively, except that they always
apply to every I/O operation and are therefore often more efficient. If
the file resides on a local storage device then when an invocation of a
method of this class returns it is guaranteed that all changes made to
the file by that invocation will have been written to that device. This
is useful for ensuring that critical information is not lost in the
event of a system crash. If the file does not reside on a local device
then no such guarantee is made.
The "rwd" mode can be used to reduce the number of I/O
operations performed. Using "rwd" only requires updates to the
file's content to be written to storage; using "rws" requires
updates to both the file's content and its metadata to be written, which
generally requires at least one more low-level I/O operation.
If there is a security manager, its checkRead
method is
called with the pathname of the file
argument as its
argument to see if read access to the file is allowed. If the mode
allows writing, the security manager's checkWrite
method is
also called with the path argument to see if write access to the file is
allowed.
Parameters:
- file - the file object
- mode - the access mode, as described
above
Throws:
- IllegalArgumentException - if the mode argument is not equal
to one of "r", "rw", "rws", or
"rwd"
- FileNotFoundException - if the mode is "r" but the given file object does
not denote an existing regular file, or if the mode begins
with "rw" but the given file object does not denote
an existing, writable regular file and a new regular file of
that name cannot be created, or if some other error occurs
while opening or creating the file
- SecurityException - if a security manager exists and its
checkRead
method denies read access to the file
or the mode is "rw" and the security manager's
checkWrite
method denies write access to the file
See Also:
SecurityManager.checkRead(java.lang.String)
,
SecurityManager.checkWrite(java.lang.String)
,
FileChannel.force(boolean)
,