BinaryReader BinaryReader BinaryReader BinaryReader Class

定义

用特定的编码将基元数据类型读作二进制值。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
继承
BinaryReaderBinaryReaderBinaryReaderBinaryReader
属性
实现

示例

下面的代码示例演示了如何存储和检索文件中的应用程序设置。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方法读取作为布尔值的下一个字节和流中的当前位置提升 1 个字节。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. 类包括支持不同的数据类型的读取的方法。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. 若要直接释放类型,调用其Dispose中的方法try / 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) BinaryReader(Stream) BinaryReader(Stream) 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(Stream, Encoding) BinaryReader(Stream, 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(Stream, Encoding, Boolean) BinaryReader(Stream, Encoding, Boolean) 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 BaseStream BaseStream BaseStream

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

方法

Close() Close() Close() Close()

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

Dispose() Dispose() Dispose() Dispose()

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

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
FillBuffer(Int32) FillBuffer(Int32) FillBuffer(Int32) FillBuffer(Int32)

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
PeekChar() PeekChar() PeekChar() PeekChar()

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

Read() Read() Read() 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) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) 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) Read(Char[], Int32, Int32) Read(Char[], Int32, Int32) 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>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)
Read(Span<Char>) Read(Span<Char>) Read(Span<Char>) Read(Span<Char>)
Read7BitEncodedInt() Read7BitEncodedInt() Read7BitEncodedInt() Read7BitEncodedInt()

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

ReadBoolean() ReadBoolean() ReadBoolean() ReadBoolean()

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

ReadByte() ReadByte() ReadByte() ReadByte()

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

ReadBytes(Int32) ReadBytes(Int32) ReadBytes(Int32) 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() ReadChar() ReadChar() 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) ReadChars(Int32) ReadChars(Int32) 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() ReadDecimal() ReadDecimal() ReadDecimal()

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

ReadDouble() ReadDouble() ReadDouble() 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() ReadInt16() ReadInt16() 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() ReadInt32() ReadInt32() 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() ReadInt64() ReadInt64() 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() ReadSByte() ReadSByte() ReadSByte()

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

ReadSingle() ReadSingle() ReadSingle() 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() ReadString() ReadString() 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() ReadUInt16() ReadUInt16() 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() ReadUInt32() ReadUInt32() ReadUInt32()

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

ReadUInt64() ReadUInt64() ReadUInt64() ReadUInt64()

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

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

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

适用于

另请参阅