FileStream.Write FileStream.Write FileStream.Write FileStream.Write Method

定义

重载

Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>)

将字节的序列从只读范围写入当前文件流,并按写入的字节数向前移动此文件流中的当前位置。Writes a sequence of bytes from a read-only span to the current file stream and advances the current position within this file stream by the number of bytes written.

Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32)

将字节块写入文件流。Writes a block of bytes to the file stream.

Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>)

将字节的序列从只读范围写入当前文件流,并按写入的字节数向前移动此文件流中的当前位置。Writes a sequence of bytes from a read-only span to the current file stream and advances the current position within this file stream by the number of bytes written.

public:
 override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write (ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))

参数

buffer
ReadOnlySpan<Byte>

内存的区域。A region of memory. 此方法将此区域的内容复制到当前文件流。This method copies the contents of this region to the current file stream.

注解

CanWrite使用属性可确定当前实例是否支持写入。Use the CanWrite property to determine whether the current instance supports writing. WriteAsync使用方法异步写入当前流。Use the WriteAsync method to write asynchronously to the current stream.

如果写入操作成功,文件流中的位置将按写入的字节数向前移动。If the write operation is successful, the position within the file stream advances by the number of bytes written. 如果发生异常,文件流中的位置将保持不变。If an exception occurs, the position within the file stream remains unchanged.

Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32)

将字节块写入文件流。Writes a block of bytes to the file stream.

public:
 override void Write(cli::array <System::Byte> ^ array, int offset, int count);
public override void Write (byte[] array, int offset, int count);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (array As Byte(), offset As Integer, count As Integer)

参数

array
Byte[]

包含要写入该流的数据的缓冲区。The buffer containing data to write to the stream.

offset
Int32 Int32 Int32 Int32

array 中的从零开始的字节偏移量,从此处开始将字节复制到该流。The zero-based byte offset in array from which to begin copying bytes to the stream.

count
Int32 Int32 Int32 Int32

最多写入的字节数。The maximum number of bytes to write.

异常

offsetcount 描述 array中的无效范围。offset and count describe an invalid range in array.

出现 I/O 错误。An I/O error occurred.

-or-

另一个线程可能导致操作系统的文件句柄的位置发生意外更改。Another thread may have caused an unexpected change in the position of the operating system's file handle.

当前的流实例不支持写入。The current stream instance does not support writing.

示例

此代码示例是为Lock方法提供的一个更大示例的一部分。This code example is part of a larger example provided for the Lock method.

// Write the original file data.
if ( fileStream->Length == 0 )
{
   tempString = String::Concat( lastRecordText, recordNumber.ToString() );
   fileStream->Write( uniEncoding->GetBytes( tempString ), 0, uniEncoding->GetByteCount( tempString ) );
}

// Write the original file data.
if(fileStream.Length == 0)
{
    tempString = 
        lastRecordText + recordNumber.ToString();
    fileStream.Write(uniEncoding.GetBytes(tempString), 
        0, uniEncoding.GetByteCount(tempString));
}
' Write the original file data.
If aFileStream.Length = 0 Then
    tempString = _
        lastRecordText + recordNumber.ToString()
    aFileStream.Write(uniEncoding.GetBytes(tempString), _
        0, uniEncoding.GetByteCount(tempString))
End If

注解

此方法重写 WriteThis method overrides Write.

参数为中的字节偏移量提供count开始array复制的位置的偏移量,而参数则提供将写入到流中的字节数。 offsetThe offset parameter gives the offset of the byte in array (the buffer index) at which to begin copying, and the count parameter gives the number of bytes that will be written to the stream. 如果写入操作成功,则流的当前位置将按写入的字节数提前。If the write operation is successful, the current position of the stream is advanced by the number of bytes written. 如果发生异常, 则流的当前位置不变。If an exception occurs, the current position of the stream is unchanged.

备注

CanWrite使用属性可确定当前实例是否支持写入。Use the CanWrite property to determine whether the current instance supports writing. 有关附加信息,请参见 CanWriteFor additional information, see CanWrite.

不要中断正在执行写操作的线程。Do not interrupt a thread that is performing a write operation. 虽然阻止该应用程序后,应用程序可能似乎已成功运行,但中断会降低应用程序的性能和可靠性。Although the application may appear to run successfully after the thread is unblocked, the interruption can decrease your application's performance and reliability.

有关常见文件和目录操作的列表,请参阅常见 I/o 任务For a list of common file and directory operations, see Common I/O Tasks.

另请参阅

适用于