A file channel is a SeekableByteChannel
that is connected to
a file. It has a current position within its file which can
be both queried
and modified
. The file itself contains a variable-length sequence
of bytes that can be read and written and whose current size
can be queried. The size of the file increases
when bytes are written beyond its current size; the size of the file
decreases when it is truncated
. The
file may also have some associated metadata such as access
permissions, content type, and last-modification time; this class does not
define methods for metadata access.
In addition to the familiar read, write, and close operations of byte channels, this class defines the following file-specific operations:
Bytes may be read
or
written
at an absolute
position in a file in a way that does not affect the channel's current
position.
A region of a file may be mapped
directly into memory; for large files this is often much more efficient
than invoking the usual read
or write
methods.
Updates made to a file may be forced
out
to the underlying storage device, ensuring that data are not
lost in the event of a system crash.
Bytes can be transferred from a file to
some other channel
, and vice
versa
, in a way that can be optimized by many operating systems
into a very fast transfer directly to or from the filesystem cache.
A region of a file may be locked
against access by other programs.
File channels are safe for use by multiple concurrent threads. The
close
method may be invoked at any time, as specified
by the Channel
interface. Only one operation that involves the
channel's position or can change its file's size may be in progress at any
given time; attempts to initiate a second such operation while the first is
still in progress will block until the first operation completes. Other
operations, in particular those that take an explicit position, may proceed
concurrently; whether they in fact do so is dependent upon the underlying
implementation and is therefore unspecified.
The view of a file provided by an instance of this class is guaranteed to be consistent with other views of the same file provided by other instances in the same program. The view provided by an instance of this class may or may not, however, be consistent with the views seen by other concurrently-running programs due to caching performed by the underlying operating system and delays induced by network-filesystem protocols. This is true regardless of the language in which these other programs are written, and whether they are running on the same machine or on some other machine. The exact nature of any such inconsistencies are system-dependent and are therefore unspecified.
A file channel is created by invoking one of the open
methods defined by this class. A file channel can also be obtained from an
existing FileInputStream
, FileOutputStream
, or RandomAccessFile
object by invoking
that object's getChannel
method, which returns a file channel that
is connected to the same underlying file. Where the file channel is obtained
from an existing stream or random access file then the state of the file
channel is intimately connected to that of the object whose getChannel
method returned the channel. Changing the channel's position, whether
explicitly or by reading or writing bytes, will change the file position of
the originating object, and vice versa. Changing the file's length via the
file channel will change the length seen via the originating object, and vice
versa. Changing the file's content by writing bytes will change the content
seen by the originating object, and vice versa. Closing the channel will
close the originating object.
At various points this class specifies that an
instance that is "open for reading," "open for writing," or "open for
reading and writing" is required. A channel obtained via the getChannel
method of a java.
instance will be open for reading. A channel
obtained via the getChannel
method of a java.
instance will be open for
writing. Finally, a channel obtained via the getChannel
method of a java.
instance will be open for reading if the instance
was created with mode "r"
and will be open for reading and writing
if the instance was created with mode "rw"
.
A file channel that is open for writing may be in
append mode, for example if it was obtained from a file-output stream
that was created by invoking the FileOutputStream(File,boolean)
constructor and passing true
for
the second parameter. In this mode each invocation of a relative write
operation 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. In this mode the behavior of the method to
write at a given position is also
system-dependent.
java.io.FileInputStream#getChannel()
, java.io.FileOutputStream#getChannel()
, java.io.RandomAccessFile#getChannel()
Modifier and Type | Class and Description |
---|---|
public static class | FileChannel.
A file-mapping mode. |
Modifier and Type | Field and Description |
---|---|
private static final FileAttribute |
Access | Constructor and Description |
---|---|
protected |
Modifier and Type | Method and Description |
---|---|
public abstract void | force(boolean
If metaData)true then this method is required to force changes
to both the file's content and metadata to be written to
storage; otherwise, it need only force content changes to be
writtenForces any updates to this channel's file to be written to the storage device that contains it. |
public abstract FileLock | Returns: A lock object representing the newly-acquired lockThe position at which the locked region is to start; must be
non-negative position, long The size of the locked region; must be non-negative, and the sum
size, boolean position + size must be non-negative.
A value of zero means to lock all bytes from the specified
starting position to the end of the file, regardless of whether
the file is subsequently extended or truncatedtrue to request a shared lock, in which case this
channel must be open for reading (and possibly writing);
false to request an exclusive lock, in which case this
channel must be open for writing (and possibly reading)Acquires a lock on the given region of this channel's file. |
public final FileLock | Returns: A lock object representing the newly-acquired lockAcquires an exclusive lock on this channel's file. |
public abstract MappedByteBuffer | Returns: The mapped byte bufferOne of the constants mode,READ_ONLY , READ_WRITE , or PRIVATE defined in the MapMode class, according to
whether the file is to be mapped read-only, read/write, or
privately (copy-on-write), respectively, or an implementation
specific map modeThe position within the file at which the mapped region
is to start; must be non-negative position, long The size of the region to be mapped; must be non-negative and
no greater than size)java. Maps a region of this channel's file directly into memory. |
public MemorySegment | Returns: A new mapped memory segmentThe file mapping mode, see
mode,FileChannel#map(FileChannel. ;
the mapping mode might affect the behavior of the returned
memory mapped segment (see MemorySegment#force() )The offset (expressed in bytes) within the file at which the
mapped segment is to start offset, long The size (in bytes) of the mapped memory backing the memory
segment size, Arena The segment arena arena)Maps a region of this channel's file into a new mapped memory segment, with the given offset, size and arena. |
public static FileChannel | Returns: A new file channelThe path of the file to open or create path, Set<? extends OpenOption> Options specifying how the file is opened options, FileAttribute<?>... An optional list of file attributes to set atomically when
creating the file attrs)Opens or creates a file, returning a file channel to access the file. |
public static FileChannel | Returns: A new file channelThe path of the file to open or create path, OpenOption... Options specifying how the file is opened options)Opens or creates a file, returning a file channel to access the file. |
public abstract long | Returns: This channel's file position, a non-negative integer counting the number of bytes from the beginning of the file to the current positionRedeclares java. Returns this channel's file position. |
public abstract FileChannel | Returns: This file channelThe new position, a non-negative integer counting
the number of bytes from the beginning of the file newPosition)Redeclares java. Sets this channel's file position. |
public abstract int | read(ByteBuffer
The buffer into which bytes are to be transferred dst)Redeclares java. Reads a sequence of bytes from this channel into the given buffer. |
public abstract long | read(ByteBuffer[]
The buffers into which bytes are to be transferred dsts, int The offset within the buffer array of the first buffer into
which bytes are to be transferred; must be non-negative and no
larger than offset, int dsts.length The maximum number of buffers to be accessed; must be
non-negative and no larger than
length)dsts.length - offset Redeclares java. Reads a sequence of bytes from this channel into a subsequence of the given buffers. |
public final long | read(ByteBuffer[]
The buffers into which bytes are to be transferred dsts)Implements java. Reads a sequence of bytes from this channel into the given buffers. |
public abstract int | Returns: The number of bytes read, possibly zero, or-1 if the
given position is greater than or equal to the file's current
sizeThe buffer into which bytes are to be transferred dst, long The file position at which the transfer is to begin;
must be non-negative position)Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. |
public abstract long | Returns: The current size of this channel's file, measured in bytesRedeclares java. Returns the current size of this channel's file. |
public abstract long | Returns: The number of bytes, possibly zero, that were actually transferredThe source channel src, long The file position at which the transfer is to begin;
must be non-negative position, long The maximum number of bytes to be transferred; must be
non-negative count)Transfers bytes into this channel's file from the given readable byte channel. |
public abstract long | Returns: The number of bytes, possibly zero, that were actually transferredThe position within the file at which the transfer is to begin;
must be non-negative position, long The maximum number of bytes to be transferred; must be
non-negative count, WritableByteChannel The target channel target)Transfers bytes from this channel's file to the given writable byte channel. |
public abstract FileChannel | Returns: This file channelThe new size, a non-negative byte count size)Redeclares java. Truncates this channel's file to the given size. |
public abstract FileLock | Returns: A lock object representing the newly-acquired lock, ornull if the lock could not be acquired
because another program holds an overlapping lockThe position at which the locked region is to start; must be
non-negative position, long The size of the locked region; must be non-negative, and the sum
size, boolean position + size must be non-negative.
A value of zero means to lock all bytes from the specified
starting position to the end of the file, regardless of whether
the file is subsequently extended or truncatedtrue to request a shared lock,
false to request an exclusive lockAttempts to acquire a lock on the given region of this channel's file. |
public final FileLock | Returns: A lock object representing the newly-acquired lock, ornull if the lock could not be acquired
because another program holds an overlapping lockAttempts to acquire an exclusive lock on this channel's file. |
public abstract int | write(ByteBuffer
The buffer from which bytes are to be retrieved src)Redeclares java. Writes a sequence of bytes to this channel from the given buffer. |
public abstract long | write(ByteBuffer[]
The buffers from which bytes are to be retrieved srcs, int The offset within the buffer array of the first buffer from
which bytes are to be retrieved; must be non-negative and no
larger than offset, int srcs.length The maximum number of buffers to be accessed; must be
non-negative and no larger than
length)srcs.length - offset Redeclares java. Writes a sequence of bytes to this channel from a subsequence of the given buffers. |
public final long | write(ByteBuffer[]
The buffers from which bytes are to be retrieved srcs)Implements java. Writes a sequence of bytes to this channel from the given buffers. |
public abstract int | Returns: The number of bytes written, possibly zeroThe buffer from which bytes are to be transferred src, long The file position at which the transfer is to begin;
must be non-negative position)Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. |