File Channel官方定义

来源:互联网 发布:微商城系统源码 编辑:程序博客网 时间:2024/06/07 20:33
public abstract class FileChannel
extends AbstractInterruptibleChannel
implements ByteChannel, GatheringByteChannel, ScatteringByteChannel

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

A file channel has a current position within its file which can be bothqueried and modified. The file itself contains a variable-length sequence of bytes that can be read and written and whose currentsize 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 istruncated. The file may also have some associatedmetadata such as access permissions, content type, and last-modification time; this class does not define methods for metadata access.

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

  • Bytes may be read or written at an absolute position in a file in a way that does not affect the channel's current position.

  • A region of a file may be mapped directly into memory; for large files this is often much more efficient than invoking the usual read orwrite methods.

  • Updates made to a file may be forced out to the underlying storage device, ensuring that data are not lost in the event of a system crash.

  • Bytes can be transferred from a file to some other channel, and vice versa, in a way that can be optimized by many operating systems into a very fast transfer directly to or from the filesystem cache.

  • A region of a file may be locked against access by other programs.

File channels are safe for use by multiple concurrent threads. Theclose 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.

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 existingFileInputStream,FileOutputStream, or RandomAccessFile 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 whosegetChannel 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 method of a FileInputStream instance will be open for reading. A channel obtained via the getChannel method of a FileOutputStream instance will be open for writing. Finally, a channel obtained via the getChannel method of a 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.

Reference URL: http://docs.oracle.com/javase/1.5.0/docs/api/java/nio/channels/FileChannel.html