System.IO.BinaryWriter.Write 方法 (Char)

方法描述

将 Unicode 字符写入当前流,并根据所使用的 Encoding 和向流中写入的特定字符,提升流的当前位置。

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

public virtual void Write(
	char ch
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
ch System-Char 要写入的非代理项 Unicode 字符。
返回值 void

提示和注释

因为存在数据格式设置冲突,所以不建议对以下编码方式使用此方法:

UTF-7

ISO-2022-JP

ISCII

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

Unicode 代理项字符必须在同一调用中成对写出,而不是单独写出。 如果您需要在应用程序中支持代理项对,请考虑使用字符数组和 Write 方法重载。

System.IO.BinaryWriter.Write 方法 (Char)例子

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

using System;
using System.IO;

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

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        for(i = 0; i < invalidPathChars.Length; i++)
        {
            binWriter.Write(invalidPathChars[i]);
        }

        // 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());
        char[] memoryData = 
            new char[memStream.Length - memStream.Position];
        for(i = 0; i < memoryData.Length; i++)
        {
            memoryData[i] = binReader.ReadChar();
        }
        Console.WriteLine(memoryData);
    }
}

异常

异常 异常描述
IOException 发生 I/O 错误。
ObjectDisposedException 流已关闭。
ArgumentException ch 是单一代理项字符。

命名空间

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 系统要求。