Partager via


FileChannel Class

Definition

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

[Android.Runtime.Register("java/nio/channels/FileChannel", DoNotGenerateAcw=true)]
public abstract class FileChannel : Java.Nio.Channels.Spi.AbstractInterruptibleChannel, IDisposable, Java.Interop.IJavaPeerable, Java.Nio.Channels.IGatheringByteChannel, Java.Nio.Channels.IScatteringByteChannel, Java.Nio.Channels.ISeekableByteChannel
[<Android.Runtime.Register("java/nio/channels/FileChannel", DoNotGenerateAcw=true)>]
type FileChannel = class
    inherit AbstractInterruptibleChannel
    interface IByteChannel
    interface IReadableByteChannel
    interface IChannel
    interface ICloseable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface IWritableByteChannel
    interface IGatheringByteChannel
    interface IScatteringByteChannel
    interface ISeekableByteChannel
Inheritance
Attributes
Implements

Remarks

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 #position() <i>queried</i> and #position(long) <i>modified</i>. The file itself contains a variable-length sequence of bytes that can be read and written and whose current #size <i>size</i> 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 #truncate <i>truncated</i>. 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:

<ul>

<li>

Bytes may be #read(ByteBuffer, long) read or #write(ByteBuffer, long) <i>written</i> at an absolute position in a file in a way that does not affect the channel's current position.

</li>

<li>

A region of a file may be #map <i>mapped</i> directly into memory; for large files this is often much more efficient than invoking the usual read or write methods.

</li>

<li>

Updates made to a file may be #force <i>forced out</i> to the underlying storage device, ensuring that data are not lost in the event of a system crash.

</li>

<li>

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

</li>

<li>

A region of a file may be FileLock <i>locked</i> against access by other programs.

</li>

</ul>

File channels are safe for use by multiple concurrent threads. The Channel#close 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 open methods defined by this class. A file channel can also be obtained from an existing java.io.FileInputStream#getChannel FileInputStream, java.io.FileOutputStream#getChannel FileOutputStream, or java.io.RandomAccessFile#getChannel 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.

"open-mode">

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

"append-mode">

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 java.io.FileOutputStream#FileOutputStream(java.io.File,boolean) 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.

Added in 1.4.

Java documentation for java.nio.channels.FileChannel.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

FileChannel()

Initializes a new instance of this class.

FileChannel(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsOpen

Returns true if this channel is open.

(Inherited from AbstractInterruptibleChannel)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Begin()

Marks the beginning of an I/O operation that might block indefinitely.

(Inherited from AbstractInterruptibleChannel)
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Close()

Closes this channel.

(Inherited from AbstractInterruptibleChannel)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
End(Boolean)

Marks the end of an I/O operation that might block indefinitely.

(Inherited from AbstractInterruptibleChannel)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Force(Boolean)

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

ForceAsync(Boolean)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
ImplCloseChannel()

Closes this channel.

(Inherited from AbstractInterruptibleChannel)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Lock()

Acquires an exclusive lock on this channel's file.

Lock(Int64, Int64, Boolean)

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

Map(FileChannel+MapMode, Int64, Int64)

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

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Open(IPath, ICollection<IOpenOption>, IFileAttribute[])

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

Open(IPath, IOpenOption[])

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

Position()

Returns this channel's file position.

Position(Int64)

Sets this channel's file position.

Read(ByteBuffer, Int64)

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

Read(ByteBuffer)

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

Read(ByteBuffer[], Int32, Int32)

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

Read(ByteBuffer[])

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

ReadAsync(ByteBuffer, Int64)
ReadAsync(ByteBuffer)
ReadAsync(ByteBuffer[], Int32, Int32)
ReadAsync(ByteBuffer[])
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
Size()

Returns the current size of this channel's file.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
TransferFrom(IReadableByteChannel, Int64, Int64)

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

TransferFromAsync(IReadableByteChannel, Int64, Int64)
TransferTo(Int64, Int64, IWritableByteChannel)

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

TransferToAsync(Int64, Int64, IWritableByteChannel)
Truncate(Int64)

Truncates this channel's file to the given size.

TruncateAsync(Int64)
TryLock()

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

TryLock(Int64, Int64, Boolean)

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

UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Write(ByteBuffer, Int64)

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

Write(ByteBuffer)

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

Write(ByteBuffer[], Int32, Int32)

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

Write(ByteBuffer[])

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

WriteAsync(ByteBuffer, Int64)
WriteAsync(ByteBuffer)
WriteAsync(ByteBuffer[], Int32, Int32)
WriteAsync(ByteBuffer[])

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)
ISeekableByteChannel.Position(Int64)
ISeekableByteChannel.Truncate(Int64)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
WriteAsync(IGatheringByteChannel, ByteBuffer[], Int32, Int32)
WriteAsync(IGatheringByteChannel, ByteBuffer[])
ReadAsync(IReadableByteChannel, ByteBuffer)
ReadAsync(IScatteringByteChannel, ByteBuffer[], Int32, Int32)
ReadAsync(IScatteringByteChannel, ByteBuffer[])
WriteAsync(IWritableByteChannel, ByteBuffer)

Applies to