System.IO.BinaryWriter.Write 方法 (Char[], Int32, Int32)

方法描述

将字符数组部分写入当前流,并根据所使用的 Encoding(可能还根据向流中写入的特定字符),提升流的当前位置。

语法定义(C# System.IO.BinaryWriter.Write 方法 (Char[], Int32, Int32) 的用法)

public virtual void Write(
	char[] chars,
	int index,
	int count
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
chars System-Char[] 包含要写入的数据的字符数组。
index System-Int32 chars 中开始写入的起始点。
count System-Int32 要写入的字符数。
返回值 void

提示和注释

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

System.IO.BinaryWriter.Write 方法 (Char[], Int32, Int32)例子

下面的代码示例说明如何通过将内存用作备份来读取和写入数据。

using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        char[] invalidPathChars = Path.InvalidPathChars;
        MemoryStream memStream = new MemoryStream();
        BinaryWriter binWriter = new BinaryWriter(memStream);

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        binWriter.Write(
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);

        // Create the reader using the same MemoryStream 
        // as used with the writer.
        BinaryReader binReader = new BinaryReader(memStream);

        // Set Position to the beginning of the stream.
        memStream.Position = 0;

        // Read the data from memory and write it to the console.
        Console.Write(binReader.ReadString());
        int arraySize = (int)(memStream.Length - memStream.Position);
        char[] memoryData = new char[arraySize];
        binReader.Read(memoryData, 0, arraySize);
        Console.WriteLine(memoryData);
    }
}

异常

异常 异常描述
ArgumentException 缓冲区长度减去 index 小于 count。
ArgumentNullException chars 为 null。
ArgumentOutOfRangeException index 或 count 为负。
IOException 发生 I/O 错误。
ObjectDisposedException 流已关闭。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。