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。

Important

此类型实现 IDisposable 接口。 使用完该类型后,应直接或间接处理它。 若要直接释放类型,请在块中Disposetry/调用其catch方法。 若要间接释放它,请使用语言构造,例如 using (在 C# 中)或 Using (在 Visual Basic 中)。 有关详细信息,请参阅接口主题中的 IDisposable “使用实现 IDisposable 的对象”部分。

构造函数

名称 说明
BinaryReader(Stream, Encoding, Boolean)

根据指定的流和字符编码初始化类的新实例 BinaryReader ,并选择性地使流保持打开状态。

BinaryReader(Stream, Encoding)

根据指定的流和字符编码初始化类的新实例 BinaryReader

BinaryReader(Stream)

基于指定的流并使用 UTF-8 编码初始化类的新实例 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 值,并将流的当前位置提升一个字节。

ReadByte()

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

ReadBytes(Int32)

将当前流中的指定字节数读入字节数组,并按该字节数向前推进当前位置。

ReadChar()

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

ReadChars(Int32)

从当前流中读取指定的字符数,返回字符数组中的数据,并根据所使用的字符和从流中读取的特定字符推进当前位置 Encoding

ReadDecimal()

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

ReadDouble()

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

ReadExactly(Span<Byte>)

从当前流中读取字节,并在填充之前 buffer 推进流中的位置。

ReadHalf()

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

ReadInt16()

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

ReadInt32()

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

ReadInt64()

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

ReadSByte()

从此流中读取已签名的字节,并将流的当前位置逐个字节提升。

ReadSingle()

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

ReadString()

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

ReadUInt16()

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

ReadUInt32()

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

ReadUInt64()

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

ToString()

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

(继承自 Object)

显式接口实现

名称 说明
IDisposable.Dispose()

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

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

适用于

另请参阅