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方法, 將下一個位元組讀取成布林值, 並將資料流程中目前的位置前移一個位元組。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. 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。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 類別所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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 的淺層複本 (Shallow Copy)。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 值,並將資料流中目前的位置往前移一個位元組。Reads a Boolean value from the current stream and advances the current position of the stream by one byte.

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

自目前資料流讀取下一個位元組,並將資料流中目前的位置往前移一個位元組。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 位元組浮點數值,並將資料流目前位置前移八個位元組。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 位元組帶正負號的整數,並將資料流目前位置前移兩個位元組。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 位元組帶正負號的整數,並將資料流目前位置前移四個位元組。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 位元組帶正負號的整數,並將資料流目前位置前移八個位元組。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()

自資料流讀取帶正負號的位元組,並將資料流目前位置前移一個位元組。Reads a signed byte from this stream and advances the current position of the stream by one byte.

ReadSingle() ReadSingle() ReadSingle() ReadSingle()

自目前資料流讀取 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. 字串會以長度為前置字元,每次以七位元編碼為一整數。The string is prefixed with the length, encoded as an integer seven bits at a time.

ReadUInt16() ReadUInt16() ReadUInt16() ReadUInt16()

以位元組由小到大的編碼方式自目前資料流讀取 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 位元組不帶正負號的整數,並將資料流目前位置前移四個位元組。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 位元組不帶正負號的整數,並將資料流目前位置前移八個位元組。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.

適用於

另請參閱