System.IO.BinaryReader.ReadBytes 方法

方法描述

从当前流中读取指定的字节数以写入字节数组中,并将当前位置前移相应的字节数。

语法定义(C# System.IO.BinaryReader.ReadBytes 方法 的用法)

public virtual byte[] ReadBytes(
	int count
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
count System-Int32 要读取的字节数。
返回值 System.Byte[] 包含从基础流中读取的数据的字节数组。 如果到达了流的末尾,则该字节数组可能小于所请求的字节数。

提示和注释

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

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

System.IO.BinaryReader.ReadBytes 方法例子

当从 ReadBytes 返回的 Byte 数组的长度为零时,检测到正在读取的文件的末尾。

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

命名空间

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