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 的物件>一節。

建構函式

名稱 Description
BinaryReader(Stream, Encoding, Boolean)

根據指定的串流與字元編碼初始化該類別的新實例 BinaryReader ,並可選擇性地保持串流開啟。

BinaryReader(Stream, Encoding)

根據指定的串流與字元編碼初始化該類別的新實例 BinaryReader

BinaryReader(Stream)

根據指定的串流並使用 UTF-8 編碼,初始化該 BinaryReader 類別的新實例。

屬性

名稱 Description
BaseStream

暴露於底層串流 BinaryReader的存取權。

方法

名稱 Description
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 位元整數。

ReadBoolean()

Boolean讀取目前串流的值,並將串流的當前位置前進一個位元組。

ReadByte()

讀取當前串流的下一個位元組,並將串流的當前位置前進一個位元組。

ReadBytes(Int32)

從目前串流讀取指定數量的位元組到位元組陣列,並將當前位置往前推進該位元組數。

ReadChar()

讀取當前串流中的下一個字元,並根據所使用的字元及被讀取的字元,推進串流 Encoding 的當前位置。

ReadChars(Int32)

從目前串流讀取指定數量的字元,回傳字元陣列中的資料,並根據所使用的字元及從串流讀取的特定字元推進當前位置 Encoding

ReadDecimal()

從目前串流讀取小數值,並將串流的當前位置往前推進十六個位元組。

ReadDouble()

從目前串流讀取一個 8 位元組的浮點數值,並將串流的當前位置往前推進 8 位元組。

ReadInt16()

從目前串流讀取一個 2 位元組的有號整數,並將串流的當前位置前進兩個位元組。

ReadInt32()

從目前串流讀取一個 4 位元組、有符號的整數,並將串流的當前位置前進四個位元組。

ReadInt64()

從目前串流讀取一個 8 位元組有符號整數,並將串流的當前位置前進 8 位元組。

ReadSByte()

讀取該串流的一個有符號位元組,並將該串流的當前位置前進一個位元組。

ReadSingle()

從目前串流讀取一個 4 位元組的浮點數值,並將串流目前位置往前推進四個位元組。

ReadString()

讀取當前串流中的一個字串。 字串前綴長度,以整數編碼,每次七位元。

ReadUInt16()

利用小端序編碼從目前串流讀取一個 2 位元組的無符號整數,並將串流位置前進兩個位元組。

ReadUInt32()

從目前串流讀取一個 4 位元組的無符號整數,並將串流位置往前推進四個位元組。

ReadUInt64()

從目前串流讀取一個 8 位元組的無符號整數,並將串流位置前進 8 位元組。

ToString()

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

(繼承來源 Object)

明確介面實作

名稱 Description
IDisposable.Dispose()

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

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

適用於

另請參閱