MemoryStream.Write 方法

定义

重载

Write(ReadOnlySpan<Byte>)

source 中包含的字节序列写入当前内存流,并按写入的字节数向前移动内存流的当前位置。Writes the sequence of bytes contained in source into the current memory stream and advances the current position within this memory stream by the number of bytes written.

Write(Byte[], Int32, Int32)

使用从缓冲区读取的数据将字节块写入当前流。Writes a block of bytes to the current stream using data read from a buffer.

Write(ReadOnlySpan<Byte>)

source 中包含的字节序列写入当前内存流,并按写入的字节数向前移动内存流的当前位置。Writes the sequence of bytes contained in source into the current memory stream and advances the current position within this memory stream by the number of bytes written.

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

参数

source
ReadOnlySpan<Byte>

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

Write(Byte[], Int32, Int32)

使用从缓冲区读取的数据将字节块写入当前流。Writes a block of bytes to the current stream using data read from a buffer.

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

参数

buffer
Byte[]

要从中写入数据的缓冲区。The buffer to write data from.

offset
Int32

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

count
Int32

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

异常

buffernullbuffer is null.

流不支持写入。The stream does not support writing. 有关其他信息,请参见 CanWriteFor additional information see CanWrite.

- 或 --or- 当前位置到流结尾的距离小于 count 字节,并且无法修改容量。The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified.

缓冲区长度减去 offset 的结果小于 countoffset subtracted from the buffer length is less than count.

offsetcount 为负。offset or count are negative.

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

当前流实例已关闭。The current stream instance is closed.

示例

此代码示例是为 MemoryStream 类提供的更大示例的一部分。This code example is part of a larger example provided for the MemoryStream class.

// Write the first string to the stream.
memStream->Write( firstString, 0, firstString->Length );
// Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length);
' Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length)

注解

此方法重写 WriteThis method overrides Write.

offset 参数提供要从中写入的 buffer 中第一个字节的偏移量,并且 count 参数提供要写入的字节数。The offset parameter gives the offset of the first byte in buffer to write from, and the count parameter gives the number of bytes to write. 如果写入操作成功,则流中的当前位置将按写入的字节数提前。If the write operation is successful, the current position within the stream is advanced by the number of bytes written. 如果发生异常,则流中的当前位置不变。If an exception occurs, the current position within the stream is unchanged.

除了使用 byte [] 参数构造的 MemoryStream 之外,在 MemoryStream 末尾编写操作的扩展 MemoryStreamExcept for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.

另请参阅

适用于