Most visited

Recently visited

Added in API level 1

FileChannel

public abstract class FileChannel
extends AbstractInterruptibleChannel implements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel

java.lang.Object
   ↳ java.nio.channels.spi.AbstractInterruptibleChannel
     ↳ java.nio.channels.FileChannel


用于读取,写入,映射和操作文件的通道。

文件通道是连接到文件的SeekableByteChannel 它的文件当前位置可以是queriedmodified 该文件本身包含一个可读写的可变长度字节序列,可以查询当前的size 当字节被写入超出其当前大小时,文件的大小增加; 文件大小为 truncated时会减少。 该文件还可能具有一些关联的元数据 ,如访问权限,内容类型和上次修改时间; 这个类没有定义元数据访问的方法。

除了字节通道的熟悉的读取,写入和关闭操作之外,该类还定义了以下特定于文件的操作:

文件通道可安全地用于多个并发线程。 close方法可以随时调用,如Channel接口所指定。 在任何给定的时间,只有一个涉及频道位置或可以更改文件大小的操作可能正在进行; 尝试在第一个操作仍在进行时启动第二个操作将会阻止,直到第一个操作完成。 其他行动,特别是那些采取明确立场的行动,可能会同时进行; 他们是否实际上这样做是依赖于基础实施,因此是未指定的。

由该类的实例提供的文件视图保证与同一程序中其他实例提供的同一文件的其他视图保持一致。 然而,由这个类的一个实例提供的视图可能会或可能不会与由其他并发运行的程序看到的视图一致,这是由于底层操作系统执行的缓存和网络文件系统协议引起的延迟。 无论编写这些其他程序的语言如何,以及它们是在同一台机器上还是在其他某台机器上运行,情况都是如此。 任何此类不一致的确切性质都与系统有关,因此未予指明。

的文件信道可以从现有的获得FileInputStreamFileOutputStream ,或RandomAccessFile对象通过调用该对象的getChannel方法,它返回被连接到相同的基本文件的文件信道。 在从现有流或随机访问文件获得文件通道的情况下,文件通道的状态与其方法返回通道的对象的状态密切相关。 改变通道的位置,无论是明确地或通过读取或写入字节,都会改变原始对象的文件位置,反之亦然。 通过文件通道更改文件的长度将改变通过原始对象看到的长度,反之亦然。 通过写入字节来更改文件的内容将改变原始对象看到的内容,反之亦然。

在不同的点上,这个类指定了一个“开放阅读”,“开放写作”或“开放阅读和写作”的实例。 通过FileInputStream实例的getChannel方法获得的通道将开放供阅读。 通过FileOutputStream实例的getChannel方法获得的通道将开放写入。 最后,通过所获得的信道getChannel一个的方法RandomAccessFile实例将是开放的,如果该实例用模式"r"创建和将开放阅读和如果该实例用模式"rw"创建写入读取。

一个打开的文件通道可能处于追加模式 ,例如,如果它是通过调用FileOutputStream(File,boolean)构造函数创建并为第二个参数传递true创建的文件输出流获得的。 在此模式下,每次调用相对写入操作时,都会先将位置前移到文件末尾,然后写入请求的数据。 位置的提升和数据的写入是否在单个原子操作中完成是依赖于系统的,因此是未指定的。

也可以看看:

Summary

Nested classes

class FileChannel.MapMode

文件映射模式的类型安全枚举。

Protected constructors

FileChannel()

初始化此类的新实例。

Public methods

abstract void force(boolean metaData)

强制将此频道文件的任何更新写入包含它的存储设备。

abstract FileLock lock(long position, long size, boolean shared)

获取此频道文件给定区域的锁定。

final FileLock lock()

获取此频道文件的独占锁定。

abstract MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)

将该频道文件的一个区域直接映射到内存中。

abstract FileChannel position(long newPosition)

设置此频道的文件位置。

abstract long position()

返回此频道的文件位置。

abstract long read(ByteBuffer[] dsts, int offset, int length)

从该通道读取一系列字节到给定缓冲区的子序列中。

abstract int read(ByteBuffer dst)

从此通道读取一系列字节到指定的缓冲区中。

final long read(ByteBuffer[] dsts)

从该通道读取一系列字节到给定的缓冲区中。

abstract int read(ByteBuffer dst, long position)

从给定的文件位置开始,从此通道中读取一系列字节到给定的缓冲区中。

abstract long size()

返回此频道文件的当前大小。

abstract long transferFrom(ReadableByteChannel src, long position, long count)

从给定的可读字节通道将字节传输到此通道的文件中。

abstract long transferTo(long position, long count, WritableByteChannel target)

将此通道文件的字节传输到给定的可写字节通道。

abstract FileChannel truncate(long size)

将此频道的文件截断为给定大小。

abstract FileLock tryLock(long position, long size, boolean shared)

尝试获取此频道文件给定区域的锁定。

final FileLock tryLock()

尝试获取此频道文件的排他锁定。

abstract int write(ByteBuffer src)

从给定缓冲区中向此通道写入一个字节序列。

abstract int write(ByteBuffer src, long position)

从给定文件位置开始,从给定缓冲区向此通道写入一个字节序列。

final long write(ByteBuffer[] srcs)

从给定的缓冲区中写入一个字节序列到这个通道。

abstract long write(ByteBuffer[] srcs, int offset, int length)

从给定缓冲区的子序列向此通道写入一个字节序列。

Inherited methods

From class java.nio.channels.spi.AbstractInterruptibleChannel
From class java.lang.Object
From interface java.nio.channels.Channel
From interface java.nio.channels.InterruptibleChannel
From interface java.nio.channels.SeekableByteChannel
From interface java.nio.channels.GatheringByteChannel
From interface java.nio.channels.ScatteringByteChannel
From interface java.io.Closeable
From interface java.nio.channels.WritableByteChannel
From interface java.nio.channels.ReadableByteChannel
From interface java.lang.AutoCloseable

Protected constructors

FileChannel

Added in API level 1
FileChannel ()

初始化此类的新实例。

Public methods

force

Added in API level 1
void force (boolean metaData)

强制将此频道文件的任何更新写入包含它的存储设备。

如果此频道的文件驻留在本地存储设备上,那么当此方法返回时,保证自该频道创建以来或者自上次调用该方法以来对该文件所做的所有更改都将被写入该设备。 这对于确保在系统崩溃时不会丢失重要信息很有用。

如果该文件没有驻留在本地设备上,则不提供这种保证。

metaData参数可用于限制此方法需要执行的I / O操作的数量。 为此参数传递false表示只需将文件内容的更新写入存储; 传递true表示必须写入对文件内容和元数据的更新,这通常至少需要一次I / O操作。 该参数实际上是否有任何影响取决于底层操作系统,因此未指定。

调用此方法可能会导致I / O操作发生,即使通道仅打开以供读取。 例如,一些操作系统会将上次访问时间保留为文件元数据的一部分,并且每次读取文件时都会更新此时间。 这是否实际完成取决于系统,因此未指定。

此方法只能保证通过此类中定义的方法强制更改此频道的文件。 它可能会也可能不会强制修改通过调用map方法获得的mapped byte buffer内容所做的更改。 调用映射字节缓冲区的force方法将强制写入对缓冲区内容所做的更改。

Parameters
metaData 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
Throws
ClosedChannelException If this channel is closed
IOException If some other I/O error occurs

lock

Added in API level 1
FileLock lock (long position, 
                long size, 
                boolean shared)

获取此频道文件给定区域的锁定。

此方法的调用将阻塞,直到该区域可被锁定,该通道关闭或调用线程中断,以先到者为准。

如果此通道在调用此方法期间被另一个线程关闭,则将引发 AsynchronousCloseException

如果调用线程在等待获取锁的时候被中断,那么它的中断状态将被设置,并且FileLockInterruptionException将被抛出。 如果在调用此方法时设置了调用者的中断状态,那么该异常将立即抛出; 线程的中断状态不会改变。

参数positionsize所指定的区域不需要包含在实际的底层文件中,甚至不会重叠。 锁区域的大小是固定的; 如果锁定的区域最初包含文件的结尾,并且文件增长超过该区域,则该文件的新部分将不会被该锁覆盖。 如果文件的大小预计会增大并且需要锁定整个文件,那么应该锁定从零开始并且不小于文件的预期最大大小的区域。 零参数lock()方法只是锁定大小为MAX_VALUE的区域。

有些操作系统不支持共享锁,在这种情况下,共享锁请求会自动转换为独占锁请求。 新获取的锁是共享的还是独占的,可以通过调用结果锁对象的方法isShared来测试。

文件锁代表整个Java虚拟机。 它们不适用于控制同一虚拟机内多个线程对文件的访问。

Parameters
position long: The position at which the locked region is to start; must be non-negative
size long: The size of the locked region; must be non-negative, and the sum position + size must be non-negative
shared 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)
Returns
FileLock A lock object representing the newly-acquired lock
Throws
IllegalArgumentException If the preconditions on the parameters do not hold
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the invoking thread is blocked in this method
FileLockInterruptionException If the invoking thread is interrupted while blocked in this method
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 If shared is true this channel was not opened for reading
NonWritableChannelException If shared is false but this channel was not opened for writing
IOException If some other I/O error occurs

也可以看看:

lock

Added in API level 1
FileLock lock ()

获取此频道文件的独占锁定。

表单 fc.lock()的这种方法的调用与调用完全相同

     fc.lock(0L, Long.MAX_VALUE, false) 

Returns
FileLock A lock object representing the newly-acquired lock
Throws
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the invoking thread is blocked in this method
FileLockInterruptionException If the invoking thread is interrupted while blocked in this method
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
NonWritableChannelException If this channel was not opened for writing
IOException If some other I/O error occurs

也可以看看:

map

Added in API level 1
MappedByteBuffer map (FileChannel.MapMode mode, 
                long position, 
                long size)

将该频道文件的一个区域直接映射到内存中。

文件的一个区域可以以三种模式之一映射到内存中:

  • 只读:任何修改结果缓冲区的尝试都会导致ReadOnlyBufferException被抛出。 MapMode.READ_ONLY

  • 读取/写入:对最终缓冲区所做的更改最终会传播到文件; 对于映射相同文件的其他程序,它们可能会或可能不会显示。 MapMode.READ_WRITE

  • 专用:对生成的缓冲区所做的更改不会传播到该文件,并且对于映射相同文件的其他程序将不可见; 相反,它们将导致创建缓冲区的修改部分的私有副本。 MapMode.PRIVATE

对于只读映射,此通道必须已打开才能阅读; 对于读/写或私人映射,此通道必须已经打开以供读取和写入。

mapped byte buffer返回的mapped byte buffer将具有零位和极限,并且容量为size ; 它的标记将是未定义的。 缓冲区及其表示的映射将保持有效,直到缓冲区本身被垃圾收集为止。

映射一旦建立,就不依赖于用来创建它的文件通道。 特别是关闭频道对映射的有效性没有影响。

内存映射文件的许多细节内在依赖于底层操作系统,因此未指定。 未指定请求区域未完全包含在此通道文件中时此方法的行为。 未指定通过此程序或其他程序对底层文件的内容或大小所做的更改是否传播至缓冲区。 未指定将缓冲区更改传播到文件的速率。

对于大多数操作系统来说,将文件映射到内存中比通过通常的readwrite方法读取或写入几十千字节的数据read write 从性能的角度来看,通常只需要将相对较大的文件映射到内存中。

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

也可以看看:

position

Added in API level 1
FileChannel position (long newPosition)

设置此频道的文件位置。

将位置设置为大于文件当前大小的值是合法的,但不会更改文件的大小。 稍后尝试在这样的位置读取字节将立即返回文件结束指示。 稍后尝试在这样的位置写入字节将导致文件生长以适应新的字节; 前一个文件结束和新写入的字节之间的任何字节值都是未指定的。

Parameters
newPosition long: The new position, a non-negative integer counting the number of bytes from the beginning of the file
Returns
FileChannel This file channel
Throws
ClosedChannelException If this channel is closed
IllegalArgumentException If the new position is negative
IOException If some other I/O error occurs

position

Added in API level 1
long position ()

返回此频道的文件位置。

Returns
long 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 If this channel is closed
IOException If some other I/O error occurs

read

Added in API level 1
long read (ByteBuffer[] dsts, 
                int offset, 
                int length)

从该通道读取一系列字节到给定缓冲区的子序列中。

从这个通道的当前文件位置开始读取字节,然后用实际读取的字节数更新文件位置。 否则,此方法的行为与ScatteringByteChannel界面中的规定完全相同。

Parameters
dsts ByteBuffer: The buffers into which bytes are to be transferred
offset 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
length int: The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
Returns
long The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws
IOException

read

Added in API level 1
int read (ByteBuffer dst)

从此通道读取一系列字节到指定的缓冲区中。

从这个通道的当前文件位置开始读取字节,然后用实际读取的字节数更新文件位置。 否则,此方法的行为与ReadableByteChannel界面中指定的ReadableByteChannel

Parameters
dst ByteBuffer: The buffer into which bytes are to be transferred
Returns
int The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws
IOException

read

Added in API level 1
long read (ByteBuffer[] dsts)

从该通道读取一系列字节到给定的缓冲区中。

从这个通道的当前文件位置开始读取字节,然后用实际读取的字节数更新文件位置。 否则,此方法的行为与ScatteringByteChannel接口中的规定完全相同。

Parameters
dsts ByteBuffer: The buffers into which bytes are to be transferred
Returns
long The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws
IOException

read

Added in API level 1
int read (ByteBuffer dst, 
                long position)

从给定的文件位置开始,从此通道中读取一系列字节到给定的缓冲区中。

此方法的工作方式与read(ByteBuffer)方法相同,只是从给定文件位置开始读取字节,而不是从通道当前位置开始读取。 此方法不会修改此频道的位置。 如果给定位置大于文件的当前大小,则不读取任何字节。

Parameters
dst ByteBuffer: The buffer into which bytes are to be transferred
position long: The file position at which the transfer is to begin; must be non-negative
Returns
int 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 If the position is negative
NonReadableChannelException If this channel was not opened for reading
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the read operation is in progress
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 If some other I/O error occurs

size

Added in API level 1
long size ()

Returns the current size of this channel's file.

Returns
long The current size of this channel's file, measured in bytes
Throws
ClosedChannelException If this channel is closed
IOException If some other I/O error occurs

transferFrom

Added in API level 1
long transferFrom (ReadableByteChannel src, 
                long position, 
                long count)

从给定的可读字节通道将字节传输到此通道的文件中。

尝试从源通道读取多达count个字节,并将它们写入该通道的文件,从给定的position开始 此方法的调用可能会或可能不会传输所有请求的字节; 不管它是否取决于渠道的性质和状态。 如果源通道的剩余字节数少于count个字节,或者源通道是非阻塞的,并且其输入缓冲区中可用的字节少于count个字节,那么将少于所请求的字节数。

此方法不会修改此频道的位置。 如果给定位置大于文件的当前大小,则不传输字节。 如果源通道有一个位置,则从该位置开始读取字节,然后该位置将增加读取的字节数。

此方法可能比从源通道读取并写入此通道的简单循环更有效。 许多操作系统可以直接从源通道将字节传输到文件系统缓存中,而无需实际复制它们。

Parameters
src ReadableByteChannel: The source channel
position long: The position within the file at which the transfer is to begin; must be non-negative
count long: The maximum number of bytes to be transferred; must be non-negative
Returns
long The number of bytes, possibly zero, that were actually transferred
Throws
IllegalArgumentException If the preconditions on the parameters do not hold
NonReadableChannelException If the source channel was not opened for reading
NonWritableChannelException If this channel was not opened for writing
ClosedChannelException If either this channel or the source channel is closed
AsynchronousCloseException If another thread closes either channel while the transfer is in progress
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 If some other I/O error occurs

transferTo

Added in API level 1
long transferTo (long position, 
                long count, 
                WritableByteChannel target)

将此通道文件的字节传输到给定的可写字节通道。

试图从该通道文件中给定的position开始读取多达count个字节,并将它们写入目标通道。 此方法的调用可能会或可能不会传输所有请求的字节; 不管它是否取决于渠道的性质和状态。 如果此通道的文件包含少于count个字节(从给定的position开始) ,或者目标通道是非阻塞的,并且其输出缓冲区中的空闲空间少于count个字节,则传输的字节数少于所请求的字节数。

此方法不会修改此频道的位置。 如果给定位置大于文件的当前大小,则不传输字节。 如果目标通道有一个位置,则从该位置开始写入字节,然后位置增加写入的字节数。

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 long: The position within the file at which the transfer is to begin; must be non-negative
count long: The maximum number of bytes to be transferred; must be non-negative
target WritableByteChannel: The target channel
Returns
long The number of bytes, possibly zero, that were actually transferred
Throws
IllegalArgumentException If the preconditions on the parameters do not hold
NonReadableChannelException If this channel was not opened for reading
NonWritableChannelException If the target channel was not opened for writing
ClosedChannelException If either this channel or the target channel is closed
AsynchronousCloseException If another thread closes either channel while the transfer is in progress
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 If some other I/O error occurs

truncate

Added in API level 1
FileChannel truncate (long size)

将此频道的文件截断为给定大小。

如果给定大小小于文件的当前大小,则文件将被截断,并丢弃超出文件新结尾的任何字节。 如果给定的大小大于或等于文件的当前大小,则文件不会被修改。 在任何一种情况下,如果此频道的文件位置大于给定的大小,则将其设置为该大小。

Parameters
size long: The new size, a non-negative byte count
Returns
FileChannel This file channel
Throws
NonWritableChannelException If this channel was not opened for writing
ClosedChannelException If this channel is closed
IllegalArgumentException If the new size is negative
IOException If some other I/O error occurs

tryLock

Added in API level 1
FileLock tryLock (long position, 
                long size, 
                boolean shared)

尝试获取此频道文件给定区域的锁定。

此方法不会阻止。 调用总是立即返回,或者获得了对所请求区域的锁定或者未能这样做。 如果由于其他程序持有重叠锁而无法获取锁,则返回null 如果由于任何其他原因未能获取锁定,则会引发相应的异常。

参数positionsize所指定的区域不需要包含在实际的底层文件中,甚至不会重叠。 锁区域的大小是固定的; 如果锁定的区域最初包含文件的结尾,并且文件增长超过该区域,则该文件的新部分将不会被该锁覆盖。 如果文件的大小预计会增大并且需要锁定整个文件,那么应该锁定从零开始并且不小于文件的预期最大大小的区域。 零参数tryLock()方法只是锁定大小为MAX_VALUE的区域。

有些操作系统不支持共享锁,在这种情况下,共享锁请求会自动转换为独占锁请求。 新获取的锁是共享的还是独占的,可以通过调用结果锁对象的方法isShared来测试。

文件锁代表整个Java虚拟机。 它们不适用于控制同一虚拟机内多个线程对文件的访问。

Parameters
position long: The position at which the locked region is to start; must be non-negative
size long: The size of the locked region; must be non-negative, and the sum position + size must be non-negative
shared boolean: true to request a shared lock, false to request an exclusive lock
Returns
FileLock 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 If the preconditions on the parameters do not hold
ClosedChannelException If this channel is closed
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 If some other I/O error occurs

也可以看看:

tryLock

Added in API level 1
FileLock tryLock ()

尝试获取此频道文件的排他锁定。

表单 fc.tryLock()的这种方法的调用的行为方式与调用完全相同

     fc.tryLock(0L, Long.MAX_VALUE, false) 

Returns
FileLock 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 If this channel is closed
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 If some other I/O error occurs

也可以看看:

write

Added in API level 1
int write (ByteBuffer src)

从给定缓冲区中向此通道写入一个字节序列。

字节从这个通道的当前文件位置开始写入,除非通道处于附加模式,在这种情况下,位置首先被提前到文件末尾。 如有必要,文件将生长以适应写入的字节,然后用实际写入的字节数更新文件位置。 否则,此方法的行为与WritableByteChannel接口所指定的WritableByteChannel

Parameters
src ByteBuffer: The buffer from which bytes are to be retrieved
Returns
int The number of bytes written, possibly zero
Throws
IOException

write

Added in API level 1
int write (ByteBuffer src, 
                long position)

从给定文件位置开始,从给定缓冲区向此通道写入一个字节序列。

此方法的工作方式与write(ByteBuffer)方法相同,只是字节从给定文件位置开始写入,而不是在通道当前位置写入。 此方法不会修改此频道的位置。 如果给定的位置大于文件的当前大小,那么文件将会增长以适应新的字节; 前一个文件结束和新写入的字节之间的任何字节值都是未指定的。

Parameters
src ByteBuffer: The buffer from which bytes are to be transferred
position long: The file position at which the transfer is to begin; must be non-negative
Returns
int The number of bytes written, possibly zero
Throws
IllegalArgumentException If the position is negative
NonWritableChannelException If this channel was not opened for writing
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the write operation is in progress
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 If some other I/O error occurs

write

Added in API level 1
long write (ByteBuffer[] srcs)

从给定的缓冲区中写入一个字节序列到这个通道。

字节从这个通道的当前文件位置开始写入,除非通道处于附加模式,在这种情况下,位置首先被提前到文件末尾。 如有必要,文件将生长以适应写入的字节,然后用实际写入的字节数更新文件位置。 否则,此方法的行为与GatheringByteChannel界面中的规定完全相同。

Parameters
srcs ByteBuffer: The buffers from which bytes are to be retrieved
Returns
long The number of bytes written, possibly zero
Throws
IOException

write

Added in API level 1
long write (ByteBuffer[] srcs, 
                int offset, 
                int length)

从给定缓冲区的子序列向此通道写入一个字节序列。

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 interface.

Parameters
srcs ByteBuffer: The buffers from which bytes are to be retrieved
offset 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
length int: The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
Returns
long The number of bytes written, possibly zero
Throws
IOException

Hooray!