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

定义

重载

Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>)
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>)

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>

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.

offset参数指定了中的字节偏移量array(缓冲区索引) 在此处开始复制,和count参数提供了将写入到流的字节数。The 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.

另请参阅

适用于