C#系列之-System.IO.BinaryReader.Read 方法 (Char[], Int32, Int32)

方法描述

从字符数组中的指定点开始,从流中读取指定的字符数。

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

public virtual int Read(
	char[] buffer,
	int index,
	int count
)

参数/返回值

参数值/返回值参数类型/返回类型参数描述/返回描述
bufferSystem-Char[]要读入数据的缓冲区。
indexSystem-Int32缓冲区中的起始点,在该处开始读入缓冲区。
countSystem-Int32要读取的字符数。
返回值System.Int32读入缓冲区的总字符数。 如果当前可用的字节没有请求的那么多,此数可能小于所请求的字符数;如果到达了流的末尾,此数可能为零。

提示和注释

BinaryReader 在读取操作失败后不会还原文件位置。

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

System.IO.BinaryReader.Read 方法 (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。
  • 要读取的解码字符数大于 count。 如果 Unicode 解码器返回回退字符或代理项对,则可能发生此情况。
ArgumentNullExceptionbuffer 为 null。
ArgumentOutOfRangeExceptionindex 或 count 为负。
ObjectDisposedException流已关闭。
IOException发生 I/O 错误。

命名空间

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值