java.lang.Object | ||
↳ | java.nio.channels.spi.AbstractInterruptibleChannel | |
↳ | java.nio.channels.FileChannel |
An abstract channel type for interaction with a platform file.
A FileChannel
defines the methods for reading, writing, memory
mapping, and manipulating the logical state of a platform file. This type
does not have a method for opening files, since this behavior has been
delegated to the FileInputStream
,
FileOutputStream
and RandomAccessFile
types.
FileChannels created from a FileInputStream
or a
RandomAccessFile
created in mode "r", are read-only. FileChannels
created from a FileOutputStream
are write-only. FileChannels created
from a RandomAccessFile
created in mode "rw" are read/write.
FileChannels created from a RandomAccessFile
that was opened in
append-mode will also be in append-mode -- meaning that each write will be
proceeded by a seek to the end of file.
FileChannels have a virtual pointer into the file which is referred to as a file position. The position can be manipulated by moving it within the file, and the current position can be queried.
FileChannels also have an associated size. The size of the file is the number of bytes that it currently contains. The size can be manipulated by adding more bytes to the end of the file (which increases the size) or truncating the file (which decreases the size). The current size can also be queried.
FileChannels have operations beyond the simple read, write, and close. They can also:
FileChannels are thread-safe. Only one operation involving manipulation of the file position may be executed at the same time. Subsequent calls to such operations will block, and one of those blocked will be freed to continue when the first operation has completed. There is no ordered queue or fairness applied to the blocked threads.
It is undefined whether operations that do not manipulate the file position will also block when there are any other operations in-flight.
The logical view of the underlying file is consistent across all FileChannels and I/O streams opened on the same file by the same virtual machine process. Therefore, modifications performed via a channel will be visible to the stream and vice versa; this includes modifications to the file position, content, size, etc.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
FileChannel.MapMode | MapMode defines file mapping mode constants. |
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Protected default constructor.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Requests that all updates to this channel are committed to the storage
device.
| |||||||||||
Obtains a lock on a specified region of the file.
| |||||||||||
Obtains an exclusive lock on this file.
| |||||||||||
Maps the file into memory.
| |||||||||||
Returns the current value of the file position pointer.
| |||||||||||
Sets the file position pointer to a new value.
| |||||||||||
Reads bytes from this file channel into a subset of the given buffers.
| |||||||||||
Reads bytes from this file channel into the given buffer.
| |||||||||||
Reads bytes from this file channel into the given buffer starting from
the specified file position.
| |||||||||||
Reads bytes from this file channel and stores them in the specified array
of buffers.
| |||||||||||
Returns the size of the file underlying this channel in bytes.
| |||||||||||
Reads up to
count bytes from src and stores them in this
channel's file starting at position . | |||||||||||
Reads up to
count bytes from this channel's file starting at
position and writes them to target . | |||||||||||
Truncates the file underlying this channel to a given size.
| |||||||||||
Attempts to acquire an exclusive lock on this file without blocking.
| |||||||||||
Attempts to acquire an exclusive lock on this file without blocking.
| |||||||||||
Attempts to write a subset of the given bytes from the buffers to this
file channel.
| |||||||||||
Writes bytes from the given buffer to this file channel starting at the
given file position.
| |||||||||||
Writes bytes from all the given byte buffers to this file channel.
| |||||||||||
Writes bytes from the given byte buffer to this file channel.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class java.nio.channels.spi.AbstractInterruptibleChannel
| |||||||||||
From class java.lang.Object
| |||||||||||
From interface java.io.Closeable
| |||||||||||
From interface java.nio.channels.Channel
| |||||||||||
From interface java.nio.channels.GatheringByteChannel
| |||||||||||
From interface java.nio.channels.InterruptibleChannel
| |||||||||||
From interface java.nio.channels.ReadableByteChannel
| |||||||||||
From interface java.nio.channels.ScatteringByteChannel
| |||||||||||
From interface java.nio.channels.WritableByteChannel
|
Requests that all updates to this channel are committed to the storage device.
When this method returns, all modifications made to the platform file underlying this channel have been committed if the file resides on a local storage device. If the file is not hosted locally, for example on a networked file system, then applications cannot be certain that the modifications have been committed.
There are no assurances given that changes made to the file using methods defined elsewhere will be committed. For example, changes made via a mapped byte buffer may not be committed.
The metadata
parameter indicates whether the update should
include the file's metadata such as last modification time, last access
time, etc. Note that passing true
may invoke an underlying
write to the operating system (if the platform is maintaining metadata
such as last access time), even if the channel is opened read-only.
metadata | true if the file metadata should be flushed in
addition to the file content, false otherwise. |
---|
ClosedChannelException | if this channel is already closed. |
---|---|
IOException | if another I/O error occurs. |
Obtains a lock on a specified region of the file.
This is the blocking version of lock acquisition, see also the
tryLock()
methods.
Attempts to acquire an overlapping lock region will fail. The attempt will fail if the overlapping lock has already been obtained, or if another thread is currently waiting to acquire the overlapping lock.
If the request is not for an overlapping lock, the thread calling this method will block until the lock is obtained (likely by no contention or another process releasing a lock), or until this thread is interrupted or the channel is closed.
If the lock is obtained successfully then the FileLock
object
returned represents the lock for subsequent operations on the locked
region.
If the thread is interrupted while waiting for the lock, the thread is
set to the interrupted state and throws a
FileLockInterruptionException
. If this channel is closed while
the thread is waiting to obtain the lock then the thread throws a
AsynchronousCloseException
.
There is no requirement for the position and size to be within the current start and length of the file.
Some platforms do not support shared locks, and if a request is made for a shared lock on such a platform, this method will attempt to acquire an exclusive lock instead. It is undefined whether the lock obtained is advisory or mandatory.
position | the starting position for the locked region. |
---|---|
size | the length of the locked region in bytes. |
shared | a flag indicating whether an attempt should be made to acquire a shared lock. |
IllegalArgumentException | if position or size is negative. |
---|---|
ClosedChannelException | if this channel is closed. |
OverlappingFileLockException | if the requested region overlaps an existing lock or pending lock request. |
NonReadableChannelException | if the channel is not opened in read-mode but shared is true. |
NonWritableChannelException | if the channel is not opened in write mode but shared is false. |
AsynchronousCloseException | if this channel is closed by another thread while this method is executing. |
FileLockInterruptionException | if the thread is interrupted while in the state of waiting on the desired file lock. |
IOException | if another I/O error occurs. |
Obtains an exclusive lock on this file.
This is a convenience method for acquiring a maximum length lock on a
file. It is equivalent to:
fileChannel.lock(0L, Long.MAX_VALUE, false);
ClosedChannelException | the file channel is closed. |
---|---|
NonWritableChannelException | this channel was not opened for writing. |
OverlappingFileLockException | either a lock is already held that overlaps this lock request, or another thread is waiting to acquire a lock that will overlap with this request. |
FileLockInterruptionException | the calling thread was interrupted while waiting to acquire the lock. |
AsynchronousCloseException | the channel was closed while the calling thread was waiting to acquire the lock. |
IOException | if another I/O error occurs while obtaining the requested lock. |
Maps the file into memory. There can be three modes: read-only, read/write and private. After mapping, changes made to memory or the file channel do not affect the other storage place.
Note: mapping a file into memory is usually expensive.
mode | one of the three mapping modes. |
---|---|
position | the starting position of the file. |
size | the size of the region to map into memory. |
NonReadableChannelException | if the FileChannel is not opened for reading but the given mode is "READ_ONLY". |
---|---|
NonWritableChannelException | if the FileChannel is not opened for writing but the given mode is not "READ_ONLY". |
IllegalArgumentException | if the given parameters of position and size are not correct.
Both must be non negative. size also must not be
bigger than max integer. |
IOException | if any I/O error occurs. |
Returns the current value of the file position pointer.
ClosedChannelException | if this channel is closed. |
---|---|
IOException | if another I/O error occurs. |
Sets the file position pointer to a new value.
The argument is the number of bytes counted from the start of the file. The position cannot be set to a value that is negative. The new position can be set beyond the current file size. If set beyond the current file size, attempts to read will return end of file. Write operations will succeed but they will fill the bytes between the current end of file and the new position with the required number of (unspecified) byte values.
offset | the new file position, in bytes. |
---|
IllegalArgumentException | if the new position is negative. |
---|---|
ClosedChannelException | if this channel is closed. |
IOException | if another I/O error occurs. |
Reads bytes from this file channel into a subset of the given buffers.
This method attempts to read all remaining()
bytes from length
byte buffers, in order, starting at targets[offset]
. It
increases the file position by the number of bytes actually read. The
number of bytes actually read is returned.
If a read operation is in progress, subsequent threads will block until the read is completed and will then contend for the ability to read.
buffers | the array of byte buffers into which the bytes will be copied. |
---|---|
start | the index of the first buffer to store bytes in. |
number | the maximum number of buffers to store bytes in. |
AsynchronousCloseException | if this channel is closed by another thread during this read operation. |
---|---|
ClosedByInterruptException | if the thread is interrupted by another thread during this read operation. |
ClosedChannelException | if this channel is closed. |
IndexOutOfBoundsException | if start < 0 or number < 0 , or if
start + number is greater than the size of
buffers . |
IOException | if another I/O error occurs; details are in the message. |
NonReadableChannelException | if the channel has not been opened in a mode that permits reading. |
Reads bytes from this file channel into the given buffer.
The maximum number of bytes that will be read is the remaining number of bytes in the buffer when the method is invoked. The bytes will be copied into the buffer starting at the buffer's current position.
The call may block if other threads are also attempting to read from this channel.
Upon completion, the buffer's position is set to the end of the bytes that have been read. The buffer's limit is not changed.
buffer | the byte buffer to receive the bytes. |
---|
AsynchronousCloseException | if another thread closes the channel during the read. |
---|---|
ClosedByInterruptException | if another thread interrupts the calling thread during the read. |
ClosedChannelException | if this channel is closed. |
IOException | if another I/O error occurs, details are in the message. |
NonReadableChannelException | if the channel has not been opened in a mode that permits reading. |
Reads bytes from this file channel into the given buffer starting from the specified file position.
The bytes are read starting at the given file position (up to the remaining number of bytes in the buffer). The number of bytes actually read is returned.
If position
is beyond the current end of file, then no bytes are
read.
Note that the file position is unmodified by this method.
buffer | the buffer to receive the bytes. |
---|---|
position | the (non-negative) position at which to read the bytes. |
AsynchronousCloseException | if this channel is closed by another thread while this method is executing. |
---|---|
ClosedByInterruptException | if another thread interrupts the calling thread while this operation is in progress. The calling thread will have the interrupt state set, and the channel will be closed. |
ClosedChannelException | if this channel is closed. |
IllegalArgumentException | if position is less than 0. |
IOException | if another I/O error occurs. |
NonReadableChannelException | if the channel has not been opened in a mode that permits reading. |
Reads bytes from this file channel and stores them in the specified array of buffers. This method attempts to read as many bytes as can be stored in the buffer array from this channel and returns the number of bytes actually read. It also increases the file position by the number of bytes read.
If a read operation is in progress, subsequent threads will block until the read is completed and will then contend for the ability to read.
Calling this method is equivalent to calling
read(buffers, 0, buffers.length);
buffers | the array of byte buffers into which the bytes will be copied. |
---|
AsynchronousCloseException | if this channel is closed by another thread during this read operation. |
---|---|
ClosedByInterruptException | if the thread is interrupted by another thread during this read operation. |
ClosedChannelException | if this channel is closed. |
IOException | if another I/O error occurs; details are in the message. |
NonReadableChannelException | if the channel has not been opened in a mode that permits reading. |
Returns the size of the file underlying this channel in bytes.
ClosedChannelException | if this channel is closed. |
---|---|
IOException | if an I/O error occurs while getting the size of the file. |
Reads up to count
bytes from src
and stores them in this
channel's file starting at position
. No bytes are transferred if
position
is larger than the size of this channel's file. Less
than count
bytes are transferred if there are less bytes
remaining in the source channel or if the source channel is non-blocking
and has less than count
bytes immediately available in its output
buffer.
Note that this channel's position is not modified.
src | the source channel to read bytes from. |
---|---|
position | the non-negative start position. |
count | the non-negative number of bytes to transfer. |
IllegalArgumentException | if the parameters are invalid. |
---|---|
NonReadableChannelException | if the source channel is not readable. |
NonWritableChannelException | if this channel is not writable. |
ClosedChannelException | if either channel has already been closed. |
AsynchronousCloseException | if either channel is closed by other threads during this operation. |
ClosedByInterruptException | if the thread is interrupted during this operation. |
IOException | if any I/O error occurs. |
Reads up to count
bytes from this channel's file starting at
position
and writes them to target
. No bytes are
transferred if position
is larger than the size of this channel's
file. Less than count
bytes are transferred if there less bytes
available from this channel's file or if the target channel is
non-blocking and has less than count
bytes free in its input
buffer.
Note that this channel's position is not modified.
position | the non-negative position to begin. |
---|---|
count | the non-negative number of bytes to transfer. |
target | the target channel to write to. |
IllegalArgumentException | if the parameters are invalid. |
---|---|
NonReadableChannelException | if this channel is not readable. |
NonWritableChannelException | if the target channel is not writable. |
ClosedChannelException | if either channel has already been closed. |
AsynchronousCloseException | if either channel is closed by other threads during this operation. |
ClosedByInterruptException | if the thread is interrupted during this operation. |
IOException | if any I/O error occurs. |
Truncates the file underlying this channel to a given size. Any bytes beyond the given size are removed from the file. If there are no bytes beyond the given size then the file contents are unmodified.
If the file position is currently greater than the given size, then it is set to the new size.
size | the maximum size of the underlying file. |
---|
IllegalArgumentException | if the requested size is negative. |
---|---|
ClosedChannelException | if this channel is closed. |
NonWritableChannelException | if the channel cannot be written to. |
IOException | if another I/O error occurs. |
Attempts to acquire an exclusive lock on this file without blocking.
This is a convenience method for attempting to acquire a maximum length
lock on the file. It is equivalent to:
fileChannel.tryLock(0L, Long.MAX_VALUE, false);
The method returns null
if the acquisition would result in an
overlapped lock with another OS process.
null
if the lock would overlap
with an existing exclusive lock in another OS process.ClosedChannelException | if the file channel is closed. |
---|---|
OverlappingFileLockException | if a lock already exists that overlaps this lock request or another thread is waiting to acquire a lock that will overlap with this request. |
IOException | if any I/O error occurs. |
Attempts to acquire an exclusive lock on this file without blocking. The
method returns null
if the acquisition would result in an
overlapped lock with another OS process.
It is possible to acquire a lock for any region even if it's completely outside of the file's size. The size of the lock is fixed. If the file grows outside of the lock that region of the file won't be locked by this lock.
position | the starting position. |
---|---|
size | the size of file to lock. |
shared | true if the lock is shared. |
null
if the lock would overlap
with an existing exclusive lock in another OS process.IllegalArgumentException | if any parameters are invalid. |
---|---|
ClosedChannelException | if the file channel is closed. |
OverlappingFileLockException | if a lock is already held that overlaps this lock request or another thread is waiting to acquire a lock that will overlap with this request. |
IOException | if any I/O error occurs. |
Attempts to write a subset of the given bytes from the buffers to this
file channel. This method attempts to write all remaining()
bytes from length
byte buffers, in order, starting at sources[offset]
. The number of bytes actually written is returned.
If a write operation is in progress, subsequent threads will block until the write is completed and then contend for the ability to write.
buffers | the array of byte buffers that is the source for bytes written to this channel. |
---|---|
offset | the index of the first buffer in buffers to get bytes
from. |
length | the number of buffers to get bytes from. |
AsynchronousCloseException | if this channel is closed by another thread during this write operation. |
---|---|
ClosedByInterruptException | if another thread interrupts the calling thread while this operation is in progress. The interrupt state of the calling thread is set and the channel is closed. |
ClosedChannelException | if this channel is closed. |
IndexOutOfBoundsException | if offset < 0 or length < 0 , or if
offset + length is greater than the size of
buffers . |
IOException | if another I/O error occurs; details are in the message. |
NonWritableChannelException | if this channel was not opened for writing. |
Writes bytes from the given buffer to this file channel starting at the given file position.
The bytes are written starting at the given file position (up to the remaining number of bytes in the buffer). The number of bytes actually written is returned.
If the position is beyond the current end of file, then the file is first extended up to the given position by the required number of unspecified byte values.
Note that the file position is not modified by this method.
buffer | the buffer containing the bytes to be written. |
---|---|
position | the (non-negative) position at which to write the bytes. |
IllegalArgumentException | if position is less than 0. |
---|---|
ClosedChannelException | if this channel is closed. |
NonWritableChannelException | if the channel was not opened in write-mode. |
AsynchronousCloseException | if this channel is closed by another thread while this method is executing. |
ClosedByInterruptException | if another thread interrupts the calling thread while this operation is in progress. The interrupt state of the calling thread is set and the channel is closed. |
IOException | if another I/O error occurs. |
Writes bytes from all the given byte buffers to this file channel.
The bytes are written starting at the current file position, and after the bytes are written (up to the remaining number of bytes in all the buffers), the file position is increased by the number of bytes actually written.
Calling this method is equivalent to calling
write(buffers, 0, buffers.length);
buffers | the buffers containing bytes to write. |
---|
AsynchronousCloseException | if this channel is closed by another thread during this write operation. |
---|---|
ClosedByInterruptException | if another thread interrupts the calling thread while this operation is in progress. The interrupt state of the calling thread is set and the channel is closed. |
ClosedChannelException | if this channel is closed. |
IOException | if another I/O error occurs; details are in the message. |
NonWritableChannelException | if this channel was not opened for writing. |
Writes bytes from the given byte buffer to this file channel.
The bytes are written starting at the current file position, and after some number of bytes are written (up to the remaining number of bytes in the buffer) the file position is increased by the number of bytes actually written.
src | the byte buffer containing the bytes to be written. |
---|
NonWritableChannelException | if the channel was not opened for writing. |
---|---|
ClosedChannelException | if the channel was already closed. |
AsynchronousCloseException | if another thread closes the channel during the write. |
ClosedByInterruptException | if another thread interrupts the calling thread while this operation is in progress. The interrupt state of the calling thread is set and the channel is closed. |
IOException | if another I/O error occurs, details are in the message. |