Stream.Write 方法

当在派生类中重写时,向当前流中写入字节序列,并将此流中的当前位置提升写入的字节数。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public MustOverride Sub Write ( _
    buffer As Byte(), _
    offset As Integer, _
    count As Integer _
)
用法
Dim instance As Stream
Dim buffer As Byte()
Dim offset As Integer
Dim count As Integer

instance.Write(buffer, offset, count)
public abstract void Write (
    byte[] buffer,
    int offset,
    int count
)
public:
virtual void Write (
    array<unsigned char>^ buffer, 
    int offset, 
    int count
) abstract
public abstract void Write (
    byte[] buffer, 
    int offset, 
    int count
)
public abstract function Write (
    buffer : byte[], 
    offset : int, 
    count : int
)

参数

  • buffer
    字节数组。此方法将 count 个字节从 buffer 复制到当前流。
  • offset
    buffer 中的从零开始的字节偏移量,从此处开始将字节复制到当前流。
  • count
    要写入当前流的字节数。

异常

异常类型 条件

ArgumentException

offset 与 count 的和大于缓冲区长度。

ArgumentNullException

buffer 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentOutOfRangeException

offset 或 count 为负。

IOException

发生 I/O 错误。

NotSupportedException

流不支持写入。

ObjectDisposedException

在流关闭后调用方法。

备注

有关创建文件和向文件中写入文本的示例,请参见 如何:向文件写入文本。有关从文件中读取文本的示例,请参见 如何:从文件读取文本。有关读取和写入二进制文件的示例,请参见 如何:对新建的数据文件进行读取和写入

使用 CanWrite 属性可确定当前实例是否支持写入。

如果写操作成功,则流中的位置将提升写入的字节数。如果出现异常,则流中的位置保持不变。

默认实现调用异步 BeginWrite 方法。

示例

下面的示例说明如何使用 Write 方法将输入流复制到输出流。

Const size As Integer = 4096
Dim bytes(4096) As Byte
Dim numBytes As Integer
numBytes = input.Read(bytes, 0, size)
While numBytes > 0
    output.Write(bytes, 0, numBytes)
    numBytes = input.Read(bytes, 0, size)
End While
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while((numBytes = input.Read(bytes, 0, size)) > 0)
    output.Write(bytes, 0, numBytes);
const int size = 4096;
array<Byte>^ bytes = gcnew array<Byte>(4096);
int numBytes;
while ( (numBytes = input->Read( bytes, 0, size )) > 0 )
      output->Write( bytes, 0, numBytes );
final int size = 4096;
ubyte bytes[] = new ubyte[4096];
int numBytes;
while (((numBytes = input.Read(bytes, 0, size)) > 0)) {
    output.Write(bytes, 0, numBytes);
} 

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Stream 类
Stream 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本