java.nio.channels
Class FileChannel

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.nio.channels.spi.AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel definition code for java.nio.channels.spi.AbstractInterruptibleChannel 
      extended by java.nio.channels.FileChannel
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , ByteChannel sample code for java.nio.channels.ByteChannel definition code for java.nio.channels.ByteChannel , Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel , GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel , InterruptibleChannel sample code for java.nio.channels.InterruptibleChannel definition code for java.nio.channels.InterruptibleChannel , ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel , ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel , WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel

public abstract class FileChannel
extends AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel definition code for java.nio.channels.spi.AbstractInterruptibleChannel
implements ByteChannel sample code for java.nio.channels.ByteChannel definition code for java.nio.channels.ByteChannel , GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel , ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel

A channel for reading, writing, mapping, and manipulating a file.

A file channel has a current position within its file which can be both queried sample code for java.nio.channels.FileChannel.position() definition code for java.nio.channels.FileChannel.position() and modified sample code for java.nio.channels.FileChannel.position(long) definition code for java.nio.channels.FileChannel.position(long) . The file itself contains a variable-length sequence of bytes that can be read and written and whose current size sample code for java.nio.channels.FileChannel.size() definition code for java.nio.channels.FileChannel.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 sample code for java.nio.channels.FileChannel.truncate(long) definition code for java.nio.channels.FileChannel.truncate(long) . 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:

File channels are safe for use by multiple concurrent threads. The close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() method may be invoked at any time, as specified by the Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.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.

This class does not define methods for opening existing files or for creating new ones; such methods may be added in a future release. In this release a file channel can be obtained from an existing FileInputStream sample code for java.io.FileInputStream.getChannel() definition code for java.io.FileInputStream.getChannel() , FileOutputStream sample code for java.io.FileOutputStream.getChannel() definition code for java.io.FileOutputStream.getChannel() , or RandomAccessFile sample code for java.io.RandomAccessFile.getChannel() definition code for java.io.RandomAccessFile.getChannel() object by invoking that object's getChannel method, which returns a file channel that is connected to the same underlying file.

The state of a 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.

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 sample code for java.io.FileInputStream.getChannel() definition code for java.io.FileInputStream.getChannel() method of a FileInputStream sample code for java.io.FileInputStream definition code for java.io.FileInputStream instance will be open for reading. A channel obtained via the getChannel sample code for java.io.FileOutputStream.getChannel() definition code for java.io.FileOutputStream.getChannel() method of a FileOutputStream sample code for java.io.FileOutputStream definition code for java.io.FileOutputStream instance will be open for writing. Finally, a channel obtained via the getChannel sample code for java.io.RandomAccessFile.getChannel() definition code for java.io.RandomAccessFile.getChannel() method of a RandomAccessFile sample code for java.io.RandomAccessFile definition code for java.io.RandomAccessFile 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) sample code for java.io.FileOutputStream.FileOutputStream(java.io.File, boolean) definition code for java.io.FileOutputStream.FileOutputStream(java.io.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.

Since:
1.4
See Also:
FileInputStream.getChannel() sample code for java.io.FileInputStream.getChannel() definition code for java.io.FileInputStream.getChannel() , FileOutputStream.getChannel() sample code for java.io.FileOutputStream.getChannel() definition code for java.io.FileOutputStream.getChannel() , RandomAccessFile.getChannel() sample code for java.io.RandomAccessFile.getChannel() definition code for java.io.RandomAccessFile.getChannel()

Nested Class Summary
static class FileChannel.MapMode sample code for java.nio.channels.FileChannel.MapMode definition code for java.nio.channels.FileChannel.MapMode
          A typesafe enumeration for file-mapping modes.
 
Constructor Summary
protected FileChannel sample code for java.nio.channels.FileChannel.FileChannel() definition code for java.nio.channels.FileChannel.FileChannel() ()
          Initializes a new instance of this class.
 
Method Summary
abstract  void force sample code for java.nio.channels.FileChannel.force(boolean) definition code for java.nio.channels.FileChannel.force(boolean) (boolean metaData)
          Forces any updates to this channel's file to be written to the storage device that contains it.
 FileLock sample code for java.nio.channels.FileLock definition code for java.nio.channels.FileLock lock sample code for java.nio.channels.FileChannel.lock() definition code for java.nio.channels.FileChannel.lock() ()
          Acquires an exclusive lock on this channel's file.
abstract  FileLock sample code for java.nio.channels.FileLock definition code for java.nio.channels.FileLock lock sample code for java.nio.channels.FileChannel.lock(long, long, boolean) definition code for java.nio.channels.FileChannel.lock(long, long, boolean) (long position, long size, boolean shared)
          Acquires a lock on the given region of this channel's file.
abstract  MappedByteBuffer sample code for java.nio.MappedByteBuffer definition code for java.nio.MappedByteBuffer map sample code for java.nio.channels.FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long) definition code for java.nio.channels.FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long) (FileChannel.MapMode sample code for java.nio.channels.FileChannel.MapMode definition code for java.nio.channels.FileChannel.MapMode  mode, long position, long size)
          Maps a region of this channel's file directly into memory.
abstract  long position sample code for java.nio.channels.FileChannel.position() definition code for java.nio.channels.FileChannel.position() ()
          Returns this channel's file position.
abstract  FileChannel sample code for java.nio.channels.FileChannel definition code for java.nio.channels.FileChannel position sample code for java.nio.channels.FileChannel.position(long) definition code for java.nio.channels.FileChannel.position(long) (long newPosition)
          Sets this channel's file position.
abstract  int read sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst)
          Reads a sequence of bytes from this channel into the given buffer.
 long read sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer[]) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts)
          Reads a sequence of bytes from this channel into the given buffers.
abstract  long read sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer[], int, int) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts, int offset, int length)
          Reads a sequence of bytes from this channel into a subsequence of the given buffers.
abstract  int read sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer, long) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer, long) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst, long position)
          Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.
abstract  long size sample code for java.nio.channels.FileChannel.size() definition code for java.nio.channels.FileChannel.size() ()
          Returns the current size of this channel's file.
abstract  long transferFrom sample code for java.nio.channels.FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long) definition code for java.nio.channels.FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long) (ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel  src, long position, long count)
          Transfers bytes into this channel's file from the given readable byte channel.
abstract  long transferTo sample code for java.nio.channels.FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) definition code for java.nio.channels.FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) (long position, long count, WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel  target)
          Transfers bytes from this channel's file to the given writable byte channel.
abstract  FileChannel sample code for java.nio.channels.FileChannel definition code for java.nio.channels.FileChannel truncate sample code for java.nio.channels.FileChannel.truncate(long) definition code for java.nio.channels.FileChannel.truncate(long) (long size)
          Truncates this channel's file to the given size.
 FileLock sample code for java.nio.channels.FileLock definition code for java.nio.channels.FileLock tryLock sample code for java.nio.channels.FileChannel.tryLock() definition code for java.nio.channels.FileChannel.tryLock() ()
          Attempts to acquire an exclusive lock on this channel's file.
abstract  FileLock sample code for java.nio.channels.FileLock definition code for java.nio.channels.FileLock tryLock sample code for java.nio.channels.FileChannel.tryLock(long, long, boolean) definition code for java.nio.channels.FileChannel.tryLock(long, long, boolean) (long position, long size, boolean shared)
          Attempts to acquire a lock on the given region of this channel's file.
abstract  int write sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src)
          Writes a sequence of bytes to this channel from the given buffer.
 long write sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer[]) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs)
          Writes a sequence of bytes to this channel from the given buffers.
abstract  long write sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer[], int, int) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs, int offset, int length)
          Writes a sequence of bytes to this channel from a subsequence of the given buffers.
abstract  int write sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer, long) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer, long) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src, long position)
          Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel definition code for java.nio.channels.spi.AbstractInterruptibleChannel
begin sample code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() , close sample code for java.nio.channels.spi.AbstractInterruptibleChannel.close() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.close() , end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) , implCloseChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.implCloseChannel() , isOpen sample code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 

Constructor Detail

FileChannel sample code for java.nio.channels.FileChannel() definition code for java.nio.channels.FileChannel()

protected FileChannel()
Initializes a new instance of this class.

Method Detail

read sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer)

public abstract int read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst)
                  throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Reads a sequence of bytes from this channel into the given buffer.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel interface.

Specified by:
read sample code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) in interface ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel
Parameters:
dst - The buffer into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

read sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer[], int, int)

public abstract long read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts,
                          int offset,
                          int length)
                   throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Reads a sequence of bytes from this channel into a subsequence of the given buffers.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel interface.

Specified by:
read sample code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[], int, int) in interface ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel
Parameters:
dsts - The buffers into which bytes are to be transferred
offset - 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 dsts.length
length - The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

read sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer[])

public final long read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts)
                throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Reads a sequence of bytes from this channel into the given buffers.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel interface.

Specified by:
read sample code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]) in interface ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel
Parameters:
dsts - The buffers into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer)

public abstract int write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src)
                   throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a sequence of bytes to this channel from the given buffer.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified by the WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel interface.

Specified by:
write sample code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) in interface WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel
Parameters:
src - The buffer from which bytes are to be retrieved
Returns:
The number of bytes written, possibly zero
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer[], int, int)

public abstract long write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs,
                           int offset,
                           int length)
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a sequence of bytes to this channel from a subsequence of the given buffers.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified in the GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel interface.

Specified by:
write sample code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[], int, int) in interface GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel
Parameters:
srcs - The buffers from which bytes are to be retrieved
offset - 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 srcs.length
length - The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
Returns:
The number of bytes written, possibly zero
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer[])

public final long write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs)
                 throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a sequence of bytes to this channel from the given buffers.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified in the GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel interface.

Specified by:
write sample code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]) in interface GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel
Parameters:
srcs - The buffers from which bytes are to be retrieved
Returns:
The number of bytes written, possibly zero
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

position sample code for java.nio.channels.FileChannel.position() definition code for java.nio.channels.FileChannel.position()

public abstract long position()
                       throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Returns this channel's file position.

Returns:
This channel's file position, a non-negative integer counting the number of bytes from the beginning of the file to the current position
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

position sample code for java.nio.channels.FileChannel.position(long) definition code for java.nio.channels.FileChannel.position(long)

public abstract FileChannel sample code for java.nio.channels.FileChannel definition code for java.nio.channels.FileChannel  position(long newPosition)
                              throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Sets this channel's file position.

Setting the position to a value that is greater than the file's current size is legal but does not change the size of the file. A later attempt to read bytes at such a position will immediately return an end-of-file indication. A later attempt to write bytes at such a position will cause the file to be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

Parameters:
newPosition - The new position, a non-negative integer counting the number of bytes from the beginning of the file
Returns:
This file channel
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the new position is negative
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

size sample code for java.nio.channels.FileChannel.size() definition code for java.nio.channels.FileChannel.size()

public abstract long size()
                   throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Returns the current size of this channel's file.

Returns:
The current size of this channel's file, measured in bytes
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

truncate sample code for java.nio.channels.FileChannel.truncate(long) definition code for java.nio.channels.FileChannel.truncate(long)

public abstract FileChannel sample code for java.nio.channels.FileChannel definition code for java.nio.channels.FileChannel  truncate(long size)
                              throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Truncates this channel's file to the given size.

If the given size is less than the file's current size then the file is truncated, discarding any bytes beyond the new end of the file. If the given size is greater than or equal to the file's current size then the file is not modified. In either case, if this channel's file position is greater than the given size then it is set to that size.

Parameters:
size - The new size, a non-negative byte count
Returns:
This file channel
Throws:
NonWritableChannelException sample code for java.nio.channels.NonWritableChannelException definition code for java.nio.channels.NonWritableChannelException - If this channel was not opened for writing
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the new size is negative
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

force sample code for java.nio.channels.FileChannel.force(boolean) definition code for java.nio.channels.FileChannel.force(boolean)

public abstract void force(boolean metaData)
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Forces any updates to this channel's file to be written to the storage device that contains it.

If this channel's file resides on a local storage device then when this method returns it is guaranteed that all changes made to the file since this channel was created, or since this method was last invoked, 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 metaData parameter can be used to limit the number of I/O operations that this method is required to perform. Passing false for this parameter indicates that only updates to the file's content need be written to storage; passing true indicates that updates to both the file's content and metadata must be written, which generally requires at least one more I/O operation. Whether this parameter actually has any effect is dependent upon the underlying operating system and is therefore unspecified.

Invoking this method may cause an I/O operation to occur even if the channel was only opened for reading. Some operating systems, for example, maintain a last-access time as part of a file's metadata, and this time is updated whenever the file is read. Whether or not this is actually done is system-dependent and is therefore unspecified.

This method is only guaranteed to force changes that were made to this channel's file via the methods defined in this class. It may or may not force changes that were made by modifying the content of a mapped byte buffer sample code for java.nio.MappedByteBuffer definition code for java.nio.MappedByteBuffer obtained by invoking the map sample code for java.nio.channels.FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long) definition code for java.nio.channels.FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long) method. Invoking the force sample code for java.nio.MappedByteBuffer.force() definition code for java.nio.MappedByteBuffer.force() method of the mapped byte buffer will force changes made to the buffer's content to be written.

Parameters:
metaData - If 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 written
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

transferTo sample code for java.nio.channels.FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel) definition code for java.nio.channels.FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)

public abstract long transferTo(long position,
                                long count,
                                WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel  target)
                         throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Transfers bytes from this channel's file to the given writable byte channel.

An attempt is made to read up to count bytes starting at the given position in this channel's file and write them to the target channel. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes are transferred if this channel's file contains fewer than count bytes starting at the given position, or if the target channel is non-blocking and it has fewer than count bytes free in its output buffer.

This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are transferred. If the target channel has a position then bytes are written starting at that position and then the position is incremented by the number of bytes written.

This method is potentially much more efficient than a simple loop that reads from this channel and writes to the target channel. Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them.

Parameters:
position - The position within the file at which the transfer is to begin; must be non-negative
count - The maximum number of bytes to be transferred; must be non-negative
target - The target channel
Returns:
The number of bytes, possibly zero, that were actually transferred
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the preconditions on the parameters do not hold
NonReadableChannelException sample code for java.nio.channels.NonReadableChannelException definition code for java.nio.channels.NonReadableChannelException - If this channel was not opened for reading
NonWritableChannelException sample code for java.nio.channels.NonWritableChannelException definition code for java.nio.channels.NonWritableChannelException - If the target channel was not opened for writing
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If either this channel or the target channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes either channel while the transfer is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the transfer is in progress, thereby closing both channels and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

transferFrom sample code for java.nio.channels.FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long) definition code for java.nio.channels.FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long)

public abstract long transferFrom(ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel  src,
                                  long position,
                                  long count)
                           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Transfers bytes into this channel's file from the given readable byte channel.

An attempt is made to read up to count bytes from the source channel and write them to this channel's file starting at the given position. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes will be transferred if the source channel has fewer than count bytes remaining, or if the source channel is non-blocking and has fewer than count bytes immediately available in its input buffer.

This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are transferred. If the source channel has a position then bytes are read starting at that position and then the position is incremented by the number of bytes read.

This method is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.

Parameters:
src - The source channel
position - The position within the file at which the transfer is to begin; must be non-negative
count - The maximum number of bytes to be transferred; must be non-negative
Returns:
The number of bytes, possibly zero, that were actually transferred
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the preconditions on the parameters do not hold
NonReadableChannelException sample code for java.nio.channels.NonReadableChannelException definition code for java.nio.channels.NonReadableChannelException - If the source channel was not opened for reading
NonWritableChannelException sample code for java.nio.channels.NonWritableChannelException definition code for java.nio.channels.NonWritableChannelException - If this channel was not opened for writing
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If either this channel or the source channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes either channel while the transfer is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the transfer is in progress, thereby closing both channels and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

read sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer, long) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer, long)

public abstract int read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst,
                         long position)
                  throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

This method works in the same manner as the read(ByteBuffer) sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer) method, except that bytes are read starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are read.

Parameters:
dst - The buffer into which bytes are to be transferred
position - The file position at which the transfer is to begin; must be non-negative
Returns:
The number of bytes read, possibly zero, or -1 if the given position is greater than or equal to the file's current size
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the position is negative
NonReadableChannelException sample code for java.nio.channels.NonReadableChannelException definition code for java.nio.channels.NonReadableChannelException - If this channel was not opened for reading
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer, long) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer, long)

public abstract int write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src,
                          long position)
                   throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

This method works in the same manner as the write(ByteBuffer) sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer) method, except that bytes are written starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than the file's current size then the file will be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

Parameters:
src - The buffer from which bytes are to be transferred
position - The file position at which the transfer is to begin; must be non-negative
Returns:
The number of bytes written, possibly zero
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the position is negative
NonWritableChannelException sample code for java.nio.channels.NonWritableChannelException definition code for java.nio.channels.NonWritableChannelException - If this channel was not opened for writing
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

map sample code for java.nio.channels.FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long) definition code for java.nio.channels.FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)

public abstract MappedByteBuffer sample code for java.nio.MappedByteBuffer definition code for java.nio.MappedByteBuffer  map(FileChannel.MapMode sample code for java.nio.channels.FileChannel.MapMode definition code for java.nio.channels.FileChannel.MapMode  mode,
                                     long position,
                                     long size)
                              throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Maps a region of this channel's file directly into memory.

A region of a file may be mapped into memory in one of three modes:

For a read-only mapping, this channel must have been opened for reading; for a read/write or private mapping, this channel must have been opened for both reading and writing.

The mapped byte buffer sample code for java.nio.MappedByteBuffer definition code for java.nio.MappedByteBuffer returned by this method will have a position of zero and a limit and capacity of size; its mark will be undefined. The buffer and the mapping that it represents will remain valid until the buffer itself is garbage-collected.

A mapping, once established, is not dependent upon the file channel that was used to create it. Closing the channel, in particular, has no effect upon the validity of the mapping.

Many of the details of memory-mapped files are inherently dependent upon the underlying operating system and are therefore unspecified. The behavior of this method when the requested region is not completely contained within this channel's file is unspecified. Whether changes made to the content or size of the underlying file, by this program or another, are propagated to the buffer is unspecified. The rate at which changes to the buffer are propagated to the file is unspecified.

For most operating systems, mapping a file into memory is more expensive than reading or writing a few tens of kilobytes of data via the usual read sample code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.FileChannel.read(java.nio.ByteBuffer) and write sample code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.FileChannel.write(java.nio.ByteBuffer) methods. From the standpoint of performance it is generally only worth mapping relatively large files into memory.

Parameters:
mode - One of the constants READ_ONLY sample code for java.nio.channels.FileChannel.MapMode.READ_ONLY definition code for java.nio.channels.FileChannel.MapMode.READ_ONLY , READ_WRITE sample code for java.nio.channels.FileChannel.MapMode.READ_WRITE definition code for java.nio.channels.FileChannel.MapMode.READ_WRITE , or PRIVATE sample code for java.nio.channels.FileChannel.MapMode.PRIVATE definition code for java.nio.channels.FileChannel.MapMode.PRIVATE defined in the FileChannel.MapMode sample code for java.nio.channels.FileChannel.MapMode definition code for java.nio.channels.FileChannel.MapMode class, according to whether the file is to be mapped read-only, read/write, or privately (copy-on-write), respectively
position - The position within the file at which the mapped region is to start; must be non-negative
size - The size of the region to be mapped; must be non-negative and no greater than Integer.MAX_VALUE sample code for java.lang.Integer.MAX_VALUE definition code for java.lang.Integer.MAX_VALUE
Throws:
NonReadableChannelException sample code for java.nio.channels.NonReadableChannelException definition code for java.nio.channels.NonReadableChannelException - If the mode is READ_ONLY sample code for java.nio.channels.FileChannel.MapMode.READ_ONLY definition code for java.nio.channels.FileChannel.MapMode.READ_ONLY but this channel was not opened for reading
NonWritableChannelException sample code for java.nio.channels.NonWritableChannelException definition code for java.nio.channels.NonWritableChannelException - If the mode is READ_WRITE sample code for java.nio.channels.FileChannel.MapMode.READ_WRITE definition code for java.nio.channels.FileChannel.MapMode.READ_WRITE or PRIVATE sample code for java.nio.channels.FileChannel.MapMode.PRIVATE definition code for java.nio.channels.FileChannel.MapMode.PRIVATE but this channel was not opened for both reading and writing
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the preconditions on the parameters do not hold
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs
See Also:
FileChannel.MapMode sample code for java.nio.channels.FileChannel.MapMode definition code for java.nio.channels.FileChannel.MapMode , MappedByteBuffer sample code for java.nio.MappedByteBuffer definition code for java.nio.MappedByteBuffer

lock sample code for java.nio.channels.FileChannel.lock(long, long, boolean) definition code for java.nio.channels.FileChannel.lock(long, long, boolean)

public abstract FileLock sample code for java.nio.channels.FileLock definition code for java.nio.channels.FileLock  lock(long position,
                              long size,
                              boolean shared)
                       throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Acquires a lock on the given region of this channel's file.

An invocation of this method will block until the region can be locked, this channel is closed, or the invoking thread is interrupted, whichever comes first.

If this channel is closed by another thread during an invocation of this method then an AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException will be thrown.

If the invoking thread is interrupted while waiting to acquire the lock then its interrupt status will be set and a FileLockInterruptionException sample code for java.nio.channels.FileLockInterruptionException definition code for java.nio.channels.FileLockInterruptionException will be thrown. If the invoker's interrupt status is set when this method is invoked then that exception will be thrown immediately; the thread's interrupt status will not be changed.

The region specified by the position and size parameters need not be contained within, or even overlap, the actual underlying file. Lock regions are fixed in size; if a locked region initially contains the end of the file and the file grows beyond the region then the new portion of the file will not be covered by the lock. If a file is expected to grow in size and a lock on the entire file is required then a region starting at zero, and no smaller than the expected maximum size of the file, should be locked. The zero-argument lock() sample code for java.nio.channels.FileChannel.lock() definition code for java.nio.channels.FileChannel.lock() method simply locks a region of size Long.MAX_VALUE sample code for java.lang.Long.MAX_VALUE definition code for java.lang.Long.MAX_VALUE .

Some operating systems do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock. Whether the newly-acquired lock is shared or exclusive may be tested by invoking the resulting lock object's isShared sample code for java.nio.channels.FileLock.isShared() definition code for java.nio.channels.FileLock.isShared() method.

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

Parameters:
position - The position at which the locked region is to start; must be non-negative
size - The size of the locked region; must be non-negative, and the sum position + size must be non-negative
shared - true 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)
Returns:
A lock object representing the newly-acquired lock
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the preconditions on the parameters do not hold
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the invoking thread is blocked in this method
FileLockInterruptionException sample code for java.nio.channels.FileLockInterruptionException definition code for java.nio.channels.FileLockInterruptionException - If the invoking thread is interrupted while blocked in this method
OverlappingFileLockException sample code for java.nio.channels.OverlappingFileLockException definition code for java.nio.channels.OverlappingFileLockException - If a lock that overlaps the requested region is already held by this Java virtual machine, or if another thread is already blocked in this method and is attempting to lock an overlapping region
NonReadableChannelException sample code for java.nio.channels.NonReadableChannelException definition code for java.nio.channels.NonReadableChannelException - If shared is true this channel was not opened for reading
NonWritableChannelException sample code for java.nio.channels.NonWritableChannelException definition code for java.nio.channels.NonWritableChannelException - If shared is false but this channel was not opened for writing
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs
See Also:
lock() sample code for java.nio.channels.FileChannel.lock() definition code for java.nio.channels.FileChannel.lock() , tryLock() sample code for java.nio.channels.FileChannel.tryLock() definition code for java.nio.channels.FileChannel.tryLock() , tryLock(long,long,boolean) sample code for java.nio.channels.FileChannel.tryLock(long, long, boolean) definition code for java.nio.channels.FileChannel.tryLock(long, long, boolean)

lock sample code for java.nio.channels.FileChannel.lock() definition code for java.nio.channels.FileChannel.lock()

public final FileLock sample code for java.nio.channels.FileLock definition code for java.nio.channels.FileLock  lock()
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Acquires an exclusive lock on this channel's file.

An invocation of this method of the form fc.lock() behaves in exactly the same way as the invocation

     fc.lock sample code for java.nio.channels.FileChannel.lock(long, long, boolean) definition code for java.nio.channels.FileChannel.lock(long, long, boolean) (0L, Long.MAX_VALUE, false) 

Returns:
A lock object representing the newly-acquired lock
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the invoking thread is blocked in this method
FileLockInterruptionException sample code for java.nio.channels.FileLockInterruptionException definition code for java.nio.channels.FileLockInterruptionException - If the invoking thread is interrupted while blocked in this method
OverlappingFileLockException sample code for java.nio.channels.OverlappingFileLockException definition code for java.nio.channels.OverlappingFileLockException - If a lock that overlaps the requested region is already held by this Java virtual machine, or if another thread is already blocked in this method and is attempting to lock an overlapping region of the same file
NonReadableChannelException sample code for java.nio.channels.NonReadableChannelException definition code for java.nio.channels.NonReadableChannelException - If shared is true this channel was not opened for reading
NonWritableChannelException sample code for java.nio.channels.NonWritableChannelException definition code for java.nio.channels.NonWritableChannelException - If shared is false but this channel was not opened for writing
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs
See Also:
lock(long,long,boolean) sample code for java.nio.channels.FileChannel.lock(long, long, boolean) definition code for java.nio.channels.FileChannel.lock(long, long, boolean) , tryLock() sample code for java.nio.channels.FileChannel.tryLock() definition code for java.nio.channels.FileChannel.tryLock() , tryLock(long,long,boolean) sample code for java.nio.channels.FileChannel.tryLock(long, long, boolean) definition code for java.nio.channels.FileChannel.tryLock(long, long, boolean)

tryLock sample code for java.nio.channels.FileChannel.tryLock(long, long, boolean) definition code for java.nio.channels.FileChannel.tryLock(long, long, boolean)

public abstract FileLock sample code for java.nio.channels.FileLock definition code for java.nio.channels.FileLock  tryLock(long position,
                                 long size,
                                 boolean shared)
                          throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Attempts to acquire a lock on the given region of this channel's file.

This method does not block. An invocation of this always returns immediately, either having acquired a lock on the requested region or having failed to do so. If it fails to acquire a lock because an overlapping lock is held by another program then it returns null. If it fails to acquire a lock for any other reason then an appropriate exception is thrown.

The region specified by the position and size parameters need not be contained within, or even overlap, the actual underlying file. Lock regions are fixed in size; if a locked region initially contains the end of the file and the file grows beyond the region then the new portion of the file will not be covered by the lock. If a file is expected to grow in size and a lock on the entire file is required then a region starting at zero, and no smaller than the expected maximum size of the file, should be locked. The zero-argument tryLock() sample code for java.nio.channels.FileChannel.tryLock() definition code for java.nio.channels.FileChannel.tryLock() method simply locks a region of size Long.MAX_VALUE sample code for java.lang.Long.MAX_VALUE definition code for java.lang.Long.MAX_VALUE .

Some operating systems do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock. Whether the newly-acquired lock is shared or exclusive may be tested by invoking the resulting lock object's isShared sample code for java.nio.channels.FileLock.isShared() definition code for java.nio.channels.FileLock.isShared() method.

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

Parameters:
position - The position at which the locked region is to start; must be non-negative
size - The size of the locked region; must be non-negative, and the sum position + size must be non-negative
shared - true to request a shared lock, false to request an exclusive lock
Returns:
A lock object representing the newly-acquired lock, or null if the lock could not be acquired because another program holds an overlapping lock
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the preconditions on the parameters do not hold
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
OverlappingFileLockException sample code for java.nio.channels.OverlappingFileLockException definition code for java.nio.channels.OverlappingFileLockException - If a lock that overlaps the requested region is already held by this Java virtual machine, or if another thread is already blocked in this method and is attempting to lock an overlapping region of the same file
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs
See Also:
lock() sample code for java.nio.channels.FileChannel.lock() definition code for java.nio.channels.FileChannel.lock() , lock(long,long,boolean) sample code for java.nio.channels.FileChannel.lock(long, long, boolean) definition code for java.nio.channels.FileChannel.lock(long, long, boolean) , tryLock() sample code for java.nio.channels.FileChannel.tryLock() definition code for java.nio.channels.FileChannel.tryLock()

tryLock sample code for java.nio.channels.FileChannel.tryLock() definition code for java.nio.channels.FileChannel.tryLock()

public final FileLock sample code for java.nio.channels.FileLock definition code for java.nio.channels.FileLock  tryLock()
                       throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Attempts to acquire an exclusive lock on this channel's file.

An invocation of this method of the form fc.tryLock() behaves in exactly the same way as the invocation

     fc.tryLock sample code for java.nio.channels.FileChannel.tryLock(long, long, boolean) definition code for java.nio.channels.FileChannel.tryLock(long, long, boolean) (0L, Long.MAX_VALUE, false) 

Returns:
A lock object representing the newly-acquired lock, or null if the lock could not be acquired because another program holds an overlapping lock
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
OverlappingFileLockException sample code for java.nio.channels.OverlappingFileLockException definition code for java.nio.channels.OverlappingFileLockException - If a lock that overlaps the requested region is already held by this Java virtual machine, or if another thread is already blocked in this method and is attempting to lock an overlapping region
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs
See Also:
lock() sample code for java.nio.channels.FileChannel.lock() definition code for java.nio.channels.FileChannel.lock() , lock(long,long,boolean) sample code for java.nio.channels.FileChannel.lock(long, long, boolean) definition code for java.nio.channels.FileChannel.lock(long, long, boolean) , tryLock(long,long,boolean) sample code for java.nio.channels.FileChannel.tryLock(long, long, boolean) definition code for java.nio.channels.FileChannel.tryLock(long, long, boolean)