Top Description Inners Fields Constructors Methods
java.nio.channels

public abstract Class FileChannel

extends AbstractInterruptibleChannel
implements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.ScatteringByteChannel, java.nio.channels.ReadableByteChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.GatheringByteChannel, java.nio.channels.WritableByteChannel, java.nio.channels.SeekableByteChannel, java.nio.channels.ByteChannel
Known Direct Subclasses
sun.nio.ch.FileChannelImpl
Imports
java.io.IOException, java.lang.foreign.Arena, .MemorySegment, java.nio.ByteBuffer, .MappedByteBuffer, java.nio.channels.spi.AbstractInterruptibleChannel, java.nio.file.FileAlreadyExistsException, .OpenOption, .Path, .StandardOpenOption, java.nio.file.attribute.FileAttribute, java.nio.file.spi.FileSystemProvider, java.util.Set, .HashSet, .Collections, jdk.internal.javac.PreviewFeature

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

A file channel is a SeekableByteChannel that is connected to a file. It has a current position within its file which can be both queried and modified. The file itself contains a variable-length sequence of bytes that can be read and written and whose current size can be queried. The size of the file increases when bytes are written beyond its current size; the size of the file decreases when it is truncated. The file may also have some associated metadata such as access permissions, content type, and last-modification time; this class does not define methods for metadata access.

In addition to the familiar read, write, and close operations of byte channels, this class defines the following file-specific operations:

File channels are safe for use by multiple concurrent threads. The close method may be invoked at any time, as specified by the Channel interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified.

The view of a file provided by an instance of this class is guaranteed to be consistent with other views of the same file provided by other instances in the same program. The view provided by an instance of this class may or may not, however, be consistent with the views seen by other concurrently-running programs due to caching performed by the underlying operating system and delays induced by network-filesystem protocols. This is true regardless of the language in which these other programs are written, and whether they are running on the same machine or on some other machine. The exact nature of any such inconsistencies are system-dependent and are therefore unspecified.

A file channel is created by invoking one of the open methods defined by this class. A file channel can also be obtained from an existing FileInputStream, FileOutputStream, or RandomAccessFile object by invoking that object's getChannel method, which returns a file channel that is connected to the same underlying file. Where the file channel is obtained from an existing stream or random access file then the state of the file channel is intimately connected to that of the object whose getChannel method returned the channel. Changing the channel's position, whether explicitly or by reading or writing bytes, will change the file position of the originating object, and vice versa. Changing the file's length via the file channel will change the length seen via the originating object, and vice versa. Changing the file's content by writing bytes will change the content seen by the originating object, and vice versa. Closing the channel will close the originating object.

At various points this class specifies that an instance that is "open for reading," "open for writing," or "open for reading and writing" is required. A channel obtained via the getChannel method of a java.io.FileInputStream instance will be open for reading. A channel obtained via the getChannel method of a java.io.FileOutputStream instance will be open for writing. Finally, a channel obtained via the getChannel method of a 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) constructor and passing true for the second parameter. In this mode each invocation of a relative write operation first advances the position to the end of the file and then writes the requested data. Whether the advancement of the position and the writing of the data are done in a single atomic operation is system-dependent and therefore unspecified. In this mode the behavior of the method to write at a given position is also system-dependent.

Authors
Mark Reinhold, Mike McCloskey, JSR-51 Expert Group
Since
1.4
See Also
java.io.FileInputStream#getChannel(), java.io.FileOutputStream#getChannel(), java.io.RandomAccessFile#getChannel()

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
FileChannel.MapMode

A file-mapping mode.

Field Summary

Modifier and TypeField and Description
private static final FileAttribute<?>[]

Constructor Summary

AccessConstructor and Description
protected
FileChannel()

Initializes a new instance of this class.

Method Summary

Modifier and TypeMethod and Description
public abstract void
force(boolean
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
metaData
)

Forces any updates to this channel's file to be written to the storage device that contains it.

public abstract FileLock

Returns:

A lock object representing the newly-acquired lock
lock
(long
The position at which the locked region is to start; must be non-negative
position
,
long
The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated
size
,
boolean
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)
shared
)

Acquires a lock on the given region of this channel's file.

public final FileLock

Returns:

A lock object representing the newly-acquired lock
lock
()

Acquires an exclusive lock on this channel's file.

public abstract MappedByteBuffer

Returns:

The mapped byte buffer
map
(FileChannel.MapMode
One of the constants READ_ONLY, READ_WRITE, or PRIVATE defined in the MapMode class, according to whether the file is to be mapped read-only, read/write, or privately (copy-on-write), respectively, or an implementation specific map mode
mode
,
long
The position within the file at which the mapped region is to start; must be non-negative
position
,
long
The size of the region to be mapped; must be non-negative and no greater than java.lang.Integer#MAX_VALUE
size
)

Maps a region of this channel's file directly into memory.

public MemorySegment

Returns:

A new mapped memory segment
map
(FileChannel.MapMode
The file mapping mode, see FileChannel#map(FileChannel.MapMode, long, long); the mapping mode might affect the behavior of the returned memory mapped segment (see MemorySegment#force())
mode
,
long
The offset (expressed in bytes) within the file at which the mapped segment is to start
offset
,
long
The size (in bytes) of the mapped memory backing the memory segment
size
,
Arena
The segment arena
arena
)

Maps a region of this channel's file into a new mapped memory segment, with the given offset, size and arena.

public static FileChannel

Returns:

A new file channel
open
(Path
The path of the file to open or create
path
,
Set<? extends OpenOption>
Options specifying how the file is opened
options
,
FileAttribute<?>...
An optional list of file attributes to set atomically when creating the file
attrs
)

Opens or creates a file, returning a file channel to access the file.

public static FileChannel

Returns:

A new file channel
open
(Path
The path of the file to open or create
path
,
OpenOption...
Options specifying how the file is opened
options
)

Opens or creates a file, returning a file channel to access the file.

public abstract long

Returns:

This channel's file position, a non-negative integer counting the number of bytes from the beginning of the file to the current position
position
()

Redeclares java.nio.channels.SeekableByteChannel.position.

Returns this channel's file position.

public abstract FileChannel

Returns:

This file channel
position
(long
The new position, a non-negative integer counting the number of bytes from the beginning of the file
newPosition
)

Redeclares java.nio.channels.SeekableByteChannel.position.

Sets this channel's file position.

public abstract int
read(ByteBuffer
The buffer into which bytes are to be transferred
dst
)

Redeclares java.nio.channels.SeekableByteChannel.read, java.nio.channels.ReadableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.

public abstract long
read(ByteBuffer[]
The buffers into which bytes are to be transferred
dsts
,
int
The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length
offset
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
length
)

Redeclares java.nio.channels.ScatteringByteChannel.read.

Reads a sequence of bytes from this channel into a subsequence of the given buffers.

public final long
read(ByteBuffer[]
The buffers into which bytes are to be transferred
dsts
)

Implements java.nio.channels.ScatteringByteChannel.read.

Reads a sequence of bytes from this channel into the given buffers.

public abstract int

Returns:

The number of bytes read, possibly zero, or -1 if the given position is greater than or equal to the file's current size
read
(ByteBuffer
The buffer into which bytes are to be transferred
dst
,
long
The file position at which the transfer is to begin; must be non-negative
position
)

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

public abstract long

Returns:

The current size of this channel's file, measured in bytes
size
()

Redeclares java.nio.channels.SeekableByteChannel.size.

Returns the current size of this channel's file.

public abstract long

Returns:

The number of bytes, possibly zero, that were actually transferred
transferFrom
(ReadableByteChannel
The source channel
src
,
long
The file position at which the transfer is to begin; must be non-negative
position
,
long
The maximum number of bytes to be transferred; must be non-negative
count
)

Transfers bytes into this channel's file from the given readable byte channel.

public abstract long

Returns:

The number of bytes, possibly zero, that were actually transferred
transferTo
(long
The position within the file at which the transfer is to begin; must be non-negative
position
,
long
The maximum number of bytes to be transferred; must be non-negative
count
,
WritableByteChannel
The target channel
target
)

Transfers bytes from this channel's file to the given writable byte channel.

public abstract FileChannel

Returns:

This file channel
truncate
(long
The new size, a non-negative byte count
size
)

Redeclares java.nio.channels.SeekableByteChannel.truncate.

Truncates this channel's file to the given size.

public abstract FileLock

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
tryLock
(long
The position at which the locked region is to start; must be non-negative
position
,
long
The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated
size
,
boolean
true to request a shared lock, false to request an exclusive lock
shared
)

Attempts to acquire a lock on the given region of this channel's file.

public final FileLock

Returns:

A lock object representing the newly-acquired lock, or null if the lock could not be acquired because another program holds an overlapping lock
tryLock
()

Attempts to acquire an exclusive lock on this channel's file.

public abstract int
write(ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Redeclares java.nio.channels.SeekableByteChannel.write, java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

public abstract long
write(ByteBuffer[]
The buffers from which bytes are to be retrieved
srcs
,
int
The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length
offset
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
length
)

Redeclares java.nio.channels.GatheringByteChannel.write.

Writes a sequence of bytes to this channel from a subsequence of the given buffers.

public final long
write(ByteBuffer[]
The buffers from which bytes are to be retrieved
srcs
)

Implements java.nio.channels.GatheringByteChannel.write.

Writes a sequence of bytes to this channel from the given buffers.

public abstract int

Returns:

The number of bytes written, possibly zero
write
(ByteBuffer
The buffer from which bytes are to be transferred
src
,
long
The file position at which the transfer is to begin; must be non-negative
position
)

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

Inherited from java.nio.channels.spi.AbstractInterruptibleChannel:
begincloseendimplCloseChannelisOpen