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 類別所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

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()

自目前資料流讀取十進位值,並將資料流的目前位置前移十六個位元組。

ReadDouble()

自目前資料流讀取 8 位元組浮點數值,並將資料流目前位置前移八個位元組。

ReadHalf()

從目前的資料流程讀取 2 位元組浮點值,並將資料流程的目前位置往前移兩個位元組。

ReadInt16()

自目前資料流讀取 2 位元組帶正負號的整數,並將資料流目前位置前移兩個位元組。

ReadInt32()

自目前資料流讀取 4 位元組帶正負號的整數,並將資料流目前位置前移四個位元組。

ReadInt64()

自目前資料流讀取 8 位元組帶正負號的整數,並將資料流目前位置前移八個位元組。

ReadSByte()

自資料流讀取帶正負號的位元組,並將資料流目前位置前移一個位元組。

ReadSingle()

自目前資料流讀取 4 位元組浮點數值,並將資料流目前位置前移四個位元組。

ReadString()

自目前資料流讀取字串。 字串會以長度為前置字元,每次以七位元編碼為一整數。

ReadUInt16()

以位元組由小到大的編碼方式自目前資料流讀取 2 位元組不帶正負號的整數,並將資料流目前位置前移兩個位元組。

ReadUInt32()

自目前資料流讀取 4 位元組不帶正負號的整數,並將資料流目前位置前移四個位元組。

ReadUInt64()

自目前資料流讀取 8 位元組不帶正負號的整數,並將資料流目前位置前移八個位元組。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IDisposable.Dispose()

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

除非另有 BinaryReader(Stream, Encoding, Boolean) 設定, BaseStream 否則會釋放 。

適用於

另請參閱