System.IO.BinaryReader.Read 方法 (Byte[], Int32, Int32)

方法描述

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

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

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

参数/返回值

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

提示和注释

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

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

System.IO.BinaryReader.Read 方法 (Byte[], Int32, Int32)例子

当 Read 方法返回零字节时,检测到正在读取的文件的末尾。

using namespace System;
using namespace System::IO;
using namespace System::Text;

public ref class DumpFileSample
{
private:
    static const int CHUNK_SIZE = 1024;

public:
    static void Main(array^ args)
    {
        try
        {
            FileStream^ fs;
            if (args->Length > 1)
            {
                fs = gcnew FileStream(args[1], FileMode::Open, FileAccess::Read);
            }
            else
            {
                throw gcnew ArgumentException();
            }
            // use one byte per character encoding for reading dump data
            BinaryReader^ br = gcnew BinaryReader(fs, gcnew ASCIIEncoding());
            array^ chunk = gcnew array(CHUNK_SIZE);
            int chunksize;

            // read file chunks until end of file
            do
            {
                chunksize = br->Read(chunk, 0, CHUNK_SIZE);
                // chunksize will be 0 if end of file is reached.
                if (chunksize > 0)
                {
                    DumpBytes(chunk, chunksize);
                }
            }
            while (chunksize > 0);
            fs->Close();
        }
        catch (...)
        {
            Console::WriteLine("Cannot find or read file to dump.");
        }
    }

    static void DumpBytes(array^ bdata, int len)
    {
        int i;
        int j = 0;
        Char dchar;
        // 3 * 16 chars for hex display, 16 chars for text and 8 chars
        // for the 'gutter' int the middle.
        StringBuilder^ dumptext = gcnew StringBuilder("        ", 16 * 4 + 8);
        for (i = 0; i < len; i++)
        {
            dumptext->Insert(j * 3, String::Format("{0:X2} ", (int)bdata[i]));
            dchar = (Char)bdata[i];
            // replace 'non-printable' chars with a '.'.
            if ( Char::IsWhiteSpace(dchar) || Char::IsControl(dchar))
            {
                dchar = '.';
            }
            dumptext->Append(dchar);
            j++;
            if (j == 16)
            {
                Console::WriteLine(dumptext);
                dumptext->Length = 0;
                dumptext->Append("        ");
                j = 0;
            }
        }
        // display the remaining line
        if (j > 0)
        {
            for (i = j; i < 16; i++)
            {
                dumptext->Insert(j * 3, "   ");
            }
            Console::WriteLine(dumptext);
        }
    }
};

int main()
{
    DumpFileSample::Main(Environment::GetCommandLineArgs());
}

异常

异常 异常描述
ArgumentException
  • 缓冲区长度减去 index 小于 count。
  • 要读取的解码字符数大于 count。 如果 Unicode 解码器返回回退字符或代理项对,则可能发生此情况。
ArgumentNullException buffer 为 null。
ArgumentOutOfRangeException index 或 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 系统要求。