BinaryReader 类

定义

以特定的编码将基元数据类型读作二进制值。Reads primitive data types as binary values in a specific encoding.

public ref class BinaryReader : IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public class BinaryReader : IDisposable
type BinaryReader = class
    interface IDisposable
Public Class BinaryReader
Implements IDisposable
继承
BinaryReader
属性
实现

示例

下面的代码示例演示如何在文件中存储和检索应用程序设置。The following code example demonstrates how to store and retrieve application settings in a file.

using System;
using System.IO;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }

    public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write(1.250F);
            writer.Write(@"c:\Temp");
            writer.Write(10);
            writer.Write(true);
        }
    }

    public static void DisplayValues()
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;

        if (File.Exists(fileName))
        {
            using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                aspectRatio = reader.ReadSingle();
                tempDirectory = reader.ReadString();
                autoSaveTime = reader.ReadInt32();
                showStatusBar = reader.ReadBoolean();
            }

            Console.WriteLine("Aspect ratio set to: " + aspectRatio);
            Console.WriteLine("Temp directory is: " + tempDirectory);
            Console.WriteLine("Auto save time set to: " + autoSaveTime);
            Console.WriteLine("Show status bar: " + showStatusBar);
        }
    }
}
Imports System.IO

Module Module1
    Const fileName As String = "AppSettings.dat"

    Sub Main()
        WriteDefaultValues()
        DisplayValues()
    End Sub

    Sub WriteDefaultValues()
        Using writer As BinaryWriter = New BinaryWriter(File.Open(fileName, FileMode.Create))
            writer.Write(1.25F)
            writer.Write("c:\Temp")
            writer.Write(10)
            writer.Write(True)
        End Using
    End Sub

    Sub DisplayValues()
        Dim aspectRatio As Single
        Dim tempDirectory As String
        Dim autoSaveTime As Integer
        Dim showStatusBar As Boolean

        If (File.Exists(fileName)) Then

            Using reader As BinaryReader = New BinaryReader(File.Open(fileName, FileMode.Open))
                aspectRatio = reader.ReadSingle()
                tempDirectory = reader.ReadString()
                autoSaveTime = reader.ReadInt32()
                showStatusBar = reader.ReadBoolean()
            End Using

            Console.WriteLine("Aspect ratio set to: " & aspectRatio)
            Console.WriteLine("Temp directory is: " & tempDirectory)
            Console.WriteLine("Auto save time set to: " & autoSaveTime)
            Console.WriteLine("Show status bar: " & showStatusBar)
        End If
    End Sub

End Module

注解

BinaryReader 类提供简化从流中读取基元数据类型的方法。The BinaryReader class provides methods that simplify reading primitive data types from a stream. 例如,可以使用 ReadBoolean 方法将下一个字节作为布尔值读取,并将流中的当前位置提升一个字节。For example, you can use the ReadBoolean method to read the next byte as a Boolean value and advance the current position in the stream by one byte. 类包含支持不同数据类型的 read 方法。The class includes read methods that support different data types.

当你创建 BinaryReader 类的新实例时,你将提供要从中读取的流,还可以选择指定编码的类型以及在释放 BinaryReader 对象后是否将流保持打开状态。When you create a new instance of the BinaryReader class, you provide the stream to read from, and optionally specify the type of encoding and whether to leave the stream open after disposing the BinaryReader object. 如果未指定编码类型,将使用 UTF-8。If you do not specify an encoding type, UTF-8 is used.

重要

此类型实现 IDisposable 接口。This type implements the IDisposable interface. 在使用完类型后,您应直接或间接释放类型。When you have finished using the type, you should dispose of it either directly or indirectly. 若要直接释放类型,请在 Disposetry/ 块中调用其 catch 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

构造函数

BinaryReader(Stream)

基于所指定的流和特定的 UTF-8 编码,初始化 BinaryReader 类的新实例。Initializes a new instance of the BinaryReader class based on the specified stream and using UTF-8 encoding.

BinaryReader(Stream, Encoding)

基于所指定的流和特定的字符编码,初始化 BinaryReader 类的新实例。Initializes a new instance of the BinaryReader class based on the specified stream and character encoding.

BinaryReader(Stream, Encoding, Boolean)

基于所提供的流和特定的字符编码,初始化 BinaryReader 类的新实例,有选择性的打开流。Initializes a new instance of the BinaryReader class based on the specified stream and character encoding, and optionally leaves the stream open.

属性

BaseStream

公开对 BinaryReader 的基础流的访问。Exposes access to the underlying stream of the BinaryReader.

方法

Close()

关闭当前阅读器及基础流。Closes the current reader and the underlying stream.

Dispose()

释放 BinaryReader 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the BinaryReader class.

Dispose(Boolean)

释放 BinaryReader 类使用的非托管资源,并可以选择释放托管资源。Releases the unmanaged resources used by the BinaryReader class and optionally releases the managed resources.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FillBuffer(Int32)

用从流中读取的指定字节数填充内部缓冲区。Fills the internal buffer with the specified number of bytes read from the stream.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
PeekChar()

返回下一个可用的字符,并且不提升字节或字符的位置。Returns the next available character and does not advance the byte or character position.

Read()

从基础流中读取字符,并根据所使用的 Encoding 和从流中读取的特定字符,提升流的当前位置。Reads characters from the underlying stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

Read(Byte[], Int32, Int32)

从字节数组中的指定点开始,从流中读取指定的字节数。Reads the specified number of bytes from the stream, starting from a specified point in the byte array.

Read(Char[], Int32, Int32)

从字符数组中的指定点开始,从流中读取指定的字符数。Reads the specified number of characters from the stream, starting from a specified point in the character array.

Read(Span<Byte>)

从当前流读取字节序列,并将流中的位置向前移动读取的字节数。Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

Read(Span<Char>)

从当前流中读取与提供的缓冲区长度相同的字符数,将其写入提供的缓冲区,然后根据所使用的 Encoding 和从流中读取的特定字符,将当前位置前移。Reads, from the current stream, the same number of characters as the length of the provided buffer, writes them in the provided buffer, and advances the current position in accordance with the Encoding used and the specific character being read from the stream.

Read7BitEncodedInt()

以压缩格式读入 32 位整数。Reads in a 32-bit integer in compressed format.

ReadBoolean()

从当前流中读取 Boolean 值,并使该流的当前位置提升 1 个字节。Reads a Boolean value from the current stream and advances the current position of the stream by one byte.

ReadByte()

从当前流中读取下一个字节,并使流的当前位置提升 1 个字节。Reads the next byte from the current stream and advances the current position of the stream by one byte.

ReadBytes(Int32)

从当前流中读取指定的字节数以写入字节数组中,并将当前位置前移相应的字节数。Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.

ReadChar()

从当前流中读取下一个字符,并根据所使用的 Encoding 和从流中读取的特定字符,提升流的当前位置。Reads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

ReadChars(Int32)

从当前流中读取指定的字符数,并以字符数组的形式返回数据,然后根据所使用的 Encoding 和从流中读取的特定字符,将当前位置前移。Reads the specified number of characters from the current stream, returns the data in a character array, and advances the current position in accordance with the Encoding used and the specific character being read from the stream.

ReadDecimal()

从当前流中读取十进制数值,并将该流的当前位置提升十六个字节。Reads a decimal value from the current stream and advances the current position of the stream by sixteen bytes.

ReadDouble()

从当前流中读取 8 字节浮点值,并使流的当前位置提升 8 个字节。Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.

ReadInt16()

从当前流中读取 2 字节有符号整数,并使流的当前位置提升 2 个字节。Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.

ReadInt32()

从当前流中读取 4 字节有符号整数,并使流的当前位置提升 4 个字节。Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.

ReadInt64()

从当前流中读取 8 字节有符号整数,并使流的当前位置提升 8 个字节。Reads an 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.

ReadSByte()

从此流中读取 1 个有符号字节,并使流的当前位置提升 1 个字节。Reads a signed byte from this stream and advances the current position of the stream by one byte.

ReadSingle()

从当前流中读取 4 字节浮点值,并使流的当前位置提升 4 个字节。Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.

ReadString()

从当前流中读取一个字符串。Reads a string from the current stream. 字符串有长度前缀,一次 7 位地被编码为整数。The string is prefixed with the length, encoded as an integer seven bits at a time.

ReadUInt16()

使用 Little-Endian 编码从当前流中读取 2 字节无符号整数,并将流的位置提升 2 个字节。Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes.

ReadUInt32()

从当前流中读取 4 字节无符号整数并使流的当前位置提升 4 个字节。Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes.

ReadUInt64()

从当前流中读取 8 字节无符号整数并使流的当前位置提升 8 个字节。Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

IDisposable.Dispose()

释放 BinaryReader 使用的所有资源。Releases all resources used by the BinaryReader.

适用于

另请参阅