Opens or creates a file, returning a file channel to access the file.
The options
parameter determines how the file is opened.
The READ
and WRITE
options determine if the file should be opened for reading and/or
writing. If neither option (or the APPEND
option) is contained in the array then the file is opened for reading.
By default reading or writing commences at the beginning of the file.
In the addition to READ
and WRITE
, the following
options may be present:
Option | Description |
---|---|
APPEND |
If this option is present then the file is opened for writing and
each invocation of the channel's write method first advances
the position to the end of the file and then writes the requested
data. Whether the advancement of the position and the writing of the
data are done in a single atomic operation is system-dependent and
therefore unspecified. This option may not be used in conjunction
with the READ or TRUNCATE_EXISTING options. |
TRUNCATE_EXISTING |
If this option is present then the existing file is truncated to a size of 0 bytes. This option is ignored when the file is opened only for reading. |
CREATE_NEW |
If this option is present then a new file is created, failing if the file already exists. When creating a file the check for the existence of the file and the creation of the file if it does not exist is atomic with respect to other file system operations. This option is ignored when the file is opened only for reading. |
CREATE |
If this option is present then an existing file is opened if it
exists, otherwise a new file is created. When creating a file the check
for the existence of the file and the creation of the file if it does
not exist is atomic with respect to other file system operations. This
option is ignored if the CREATE_NEW option is also present or
the file is opened only for reading. |
DELETE_ON_CLOSE |
When this option is present then the implementation makes a
best effort attempt to delete the file when closed by
the close method. If the close method is not
invoked then a best effort attempt is made to delete the file
when the Java virtual machine terminates. |
SPARSE |
When creating a new file this option is a hint that the new file will be sparse. This option is ignored when not creating a new file. |
SYNC |
Requires that every update to the file's content or metadata be written synchronously to the underlying storage device. (see Synchronized I/O file integrity). |
DSYNC |
Requires that every update to the file's content be written synchronously to the underlying storage device. (see Synchronized I/O file integrity). |
An implementation may also support additional options.
The attrs
parameter is an optional array of file file-attributes
to set atomically when creating the file.
The new channel is created by invoking the newFileChannel
method on the
provider that created the Path
.
path
| The path of the file to open or create | |
options
| Options specifying how the file is opened | |
attrs
| An optional list of file attributes to set atomically when creating the file |
IllegalArgumentException
| If the set contains an invalid combination of options | |
UnsupportedOperationException
| If the path is associated with a provider that does not
support creating file channels, or an unsupported open option is
specified, or the array contains an attribute that cannot be set
atomically when creating the file | |
IOException
| If an I/O error occurs | |
SecurityException
| If a security manager is installed and it denies an
unspecified permission required by the implementation.
In the case of the default provider, the SecurityManager.checkRead(String) method is invoked to check
read access if the file is opened for reading. The SecurityManager.checkWrite(String) method is invoked to check
write access if the file is opened for writing |
Diagram: FileChannel