BinaryReader 类

定义

用特定的编码将基元数据类型读作二进制值。

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

示例

下面的代码示例演示如何在文件中存储和检索应用程序设置。

using System;
using System.IO;
using System.Text;

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

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

    public static void WriteDefaultValues()
    {
        using (var stream = File.Open(fileName, FileMode.Create))
        {
            using (var writer = new BinaryWriter(stream, Encoding.UTF8, false))
            {
                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 (var stream = File.Open(fileName, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
                {
                    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);
        }
    }
}
open System.IO
open System.Text

let fileName = "AppSettings.dat"

let writeDefaultValues () =
    use stream = File.Open(fileName, FileMode.Create)
    use writer = new BinaryWriter(stream, Encoding.UTF8, false)
    writer.Write 1.250F
    writer.Write @"c:\Temp"
    writer.Write 10
    writer.Write true

let displayValues () =
    if File.Exists fileName then
        use stream = File.Open(fileName, FileMode.Open)
        use reader = new BinaryReader(stream, Encoding.UTF8, false)
        let aspectRatio = reader.ReadSingle()
        let tempDirectory = reader.ReadString()
        let autoSaveTime = reader.ReadInt32()
        let showStatusBar = reader.ReadBoolean()

        printfn $"Aspect ratio set to: {aspectRatio}"
        printfn $"Temp directory is: {tempDirectory}"
        printfn $"Auto save time set to: {autoSaveTime}"
        printfn $"Show status bar: {showStatusBar}"

writeDefaultValues ()
displayValues ()
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 类提供的方法可简化从流中读取基元数据类型的方法。 例如,可以使用 ReadBoolean 该方法将下一个字节读取为布尔值,并将流中的当前位置向前推进一个字节。 该类包括支持不同数据类型的读取方法。

创建类的新实例 BinaryReader 时,提供要从中读取的流,并选择性地指定编码的类型,以及释放对象后 BinaryReader 是否让流保持打开状态。 如果未指定编码类型,则使用 UTF-8。

重要

此类型实现 IDisposable 接口。 在使用完类型后,您应直接或间接释放类型。 若要直接释放类型,请在 try/catch 块中调用其 Dispose 方法。 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。

构造函数

BinaryReader(Stream)

基于所指定的流和特定的 UTF-8 编码,初始化 BinaryReader 类的新实例。

BinaryReader(Stream, Encoding)

基于所指定的流和特定的字符编码,初始化 BinaryReader 类的新实例。

BinaryReader(Stream, Encoding, Boolean)

基于所提供的流和特定的字符编码,初始化 BinaryReader 类的新实例,有选择性的打开流。

属性

BaseStream

公开对 BinaryReader 的基础流的访问。

方法

Close()

关闭当前阅读器及基础流。

Dispose()

释放 BinaryReader 类的当前实例所使用的所有资源。

Dispose(Boolean)

释放 BinaryReader 类使用的非托管资源,并可以选择释放托管资源。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
FillBuffer(Int32)

用从流中读取的指定字节数填充内部缓冲区。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
PeekChar()

返回下一个可用的字符,并且不提升字节或字符的位置。

Read()

从基础流中读取字符,并根据所使用的 Encoding 和从流中读取的特定字符,提升流的当前位置。

Read(Byte[], Int32, Int32)

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

Read(Char[], Int32, Int32)

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

Read(Span<Byte>)

从当前流读取字节序列,并将流中的位置向前移动读取的字节数。

Read(Span<Char>)

从当前流中读取与提供的缓冲区长度相同的字符数,将其写入提供的缓冲区,然后根据所使用的 Encoding 和从流中读取的特定字符,将当前位置前移。

Read7BitEncodedInt()

以压缩格式读入 32 位整数。

Read7BitEncodedInt64()

一次读取一个 7 位数字。

ReadBoolean()

从当前流中读取 Boolean 值,并使该流的当前位置提升 1 个字节。

ReadByte()

从当前流中读取下一个字节,并使流的当前位置提升 1 个字节。

ReadBytes(Int32)

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

ReadChar()

从当前流中读取下一个字符,并根据所使用的 Encoding 和从流中读取的特定字符,提升流的当前位置。

ReadChars(Int32)

从当前流中读取指定的字符数,并以字符数组的形式返回数据,然后根据所使用的 Encoding 和从流中读取的特定字符,将当前位置前移。

ReadDecimal()

从当前流中读取十进制数值,并将该流的当前位置提升十六个字节。

ReadDouble()

从当前流中读取 8 字节浮点值,并使流的当前位置提升 8 个字节。

ReadHalf()

从当前流中读取 2 字节浮点值,并将流的当前位置提升两个字节。

ReadInt16()

从当前流中读取 2 字节有符号整数,并使流的当前位置提升 2 个字节。

ReadInt32()

从当前流中读取 4 字节有符号整数,并使流的当前位置提升 4 个字节。

ReadInt64()

从当前流中读取 8 字节有符号整数,并使流的当前位置提升 8 个字节。

ReadSByte()

从此流中读取 1 个有符号字节,并使流的当前位置提升 1 个字节。

ReadSingle()

从当前流中读取 4 字节浮点值,并使流的当前位置提升 4 个字节。

ReadString()

从当前流中读取一个字符串。 字符串有长度前缀,一次 7 位地被编码为整数。

ReadUInt16()

使用 Little-Endian 编码从当前流中读取 2 字节无符号整数,并将流的位置提升 2 个字节。

ReadUInt32()

从当前流中读取 4 字节无符号整数并使流的当前位置提升 4 个字节。

ReadUInt64()

从当前流中读取 8 字节无符号整数并使流的当前位置提升 8 个字节。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

IDisposable.Dispose()

此 API 支持产品基础结构,不能在代码中直接使用。

BaseStream除非按其他方式BinaryReader(Stream, Encoding, Boolean)配置,否则发布 。

适用于

另请参阅