BinaryWriter 類別

定義

以二進位方式將基本型別 (Primitive Type) 寫入資料流,並支援以特定編碼方式寫入字串。Writes primitive types in binary to a stream and supports writing strings in a specific encoding.

public ref class BinaryWriter : IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class BinaryWriter : IDisposable
type BinaryWriter = class
    interface IDisposable
Public Class BinaryWriter
Implements IDisposable
繼承
BinaryWriter
屬性
實作

範例

下列程式碼範例示範如何在檔案中儲存和取出應用程式設定。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

備註

BinaryWriter類別提供可簡化將基本資料類型寫入資料流程的方法。The BinaryWriter class provides methods that simplify writing primitive data types to a stream. 例如, 您可以使用Write方法, 將布林值當做單一位元組值寫入資料流程。For example, you can use the Write method to write a Boolean value to the stream as a one-byte value. 類別包含支援不同資料類型的寫入方法。The class includes write methods that support different data types.

當您建立BinaryWriter類別的新實例時, 您會提供要寫入的資料流程, 並選擇性地指定編碼類型, 以及是否要在BinaryWriter處置物件之後讓資料流程保持開啟。When you create a new instance of the BinaryWriter class, you provide the stream to write to, and optionally specify the type of encoding and whether to leave the stream open after disposing the BinaryWriter 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.

衍生類別可以覆寫這個類別的方法, 以提供唯一的字元編碼。A derived class can override the methods of this class to give unique character encodings.

建構函式

BinaryWriter()

初始化 BinaryWriter 類別的新執行個體,其將寫入資料流。Initializes a new instance of the BinaryWriter class that writes to a stream.

BinaryWriter(Stream)

根據指定的資料流並使用 UTF-8 編碼方式,初始化 BinaryWriter 類別的新執行個體。Initializes a new instance of the BinaryWriter class based on the specified stream and using UTF-8 encoding.

BinaryWriter(Stream, Encoding)

根據指定的資料流和字元編碼,初始化 BinaryWriter 類別的新執行個體。Initializes a new instance of the BinaryWriter class based on the specified stream and character encoding.

BinaryWriter(Stream, Encoding, Boolean)

根據指定的資料流和特定的字元編碼,初始化 BinaryWriter 類別的新執行個體,並選擇性地保留資料流開啟狀態。Initializes a new instance of the BinaryWriter class based on the specified stream and character encoding, and optionally leaves the stream open.

欄位

Null

指定沒有備份存放區的 BinaryWriterSpecifies a BinaryWriter with no backing store.

OutStream

容納基礎資料流。Holds the underlying stream.

屬性

BaseStream

取得 BinaryWriter 的基礎資料流。Gets the underlying stream of the BinaryWriter.

方法

Close()

關閉目前 BinaryWriter 和基礎資料流。Closes the current BinaryWriter and the underlying stream.

Dispose()

釋放 BinaryWriter 類別目前的執行個體所使用的全部資源。Releases all resources used by the current instance of the BinaryWriter class.

Dispose(Boolean)

釋放 BinaryWriter 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the BinaryWriter and optionally releases the managed resources.

DisposeAsync()

以非同步方式將 BinaryWriter 類別的目前執行個體所使用所有資源釋出。Asynchronously releases all resources used by the current instance of the BinaryWriter class.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
Flush()

清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Seek(Int32, SeekOrigin)

設定目前資料流位置。Sets the position within the current stream.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
Write(Boolean)

寫入一位元組的 Boolean 值至目前資料流,其中 0 表示 false,1 表示 trueWrites a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.

Write(Byte)

將不帶正負號 (Unsigned) 位元組寫入目前資料流,並將資料流位置前移一個位元組。Writes an unsigned byte to the current stream and advances the stream position by one byte.

Write(Byte[])

將位元組陣列寫入基礎資料流。Writes a byte array to the underlying stream.

Write(Byte[], Int32, Int32)

將一個區域的位元組陣列寫入目前資料流。Writes a region of a byte array to the current stream.

Write(Char)

將 Unicode 字元寫入至目前資料流,並按照所使用的 Encoding 和寫入資料流的特定字元,將資料流中目前的位置往前移。Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

Write(Char[])

將字元陣列寫入至目前資料流,並按照所使用的 Encoding 和寫入資料流的特定字元,將資料流中目前的位置往前移。Writes a character array to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

Write(Char[], Int32, Int32)

將字元陣列中的一區段寫入至目前的資料流,並按照所使用的 Encoding 和可能為寫入資料流的特定字元,將資料流中目前的位置往前移。Writes a section of a character array to the current stream, and advances the current position of the stream in accordance with the Encoding used and perhaps the specific characters being written to the stream.

Write(Decimal)

將十進位值寫入目前的資料流,並將資料流位置往前移十六個位元組。Writes a decimal value to the current stream and advances the stream position by sixteen bytes.

Write(Double)

將八位元組浮點數值寫入目前資料流,並將資料流目前位置前移八個位元組。Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.

Write(Int16)

將二位元組帶正負號的整數 (Signed Integer) 寫入目前資料流,並將資料流目前位置前移兩個位元組。Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.

Write(Int32)

將四位元組帶正負號的整數寫入目前資料流,並將資料流目前位置前移四個位元組。Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.

Write(Int64)

將八位元組帶正負號的整數寫入目前資料流,並將資料流目前位置前移八個位元組。Writes an eight-byte signed integer to the current stream and advances the stream position by eight bytes.

Write(ReadOnlySpan<Byte>)

將位元組範圍寫入目前的資料流。Writes a span of bytes to the current stream.

Write(ReadOnlySpan<Char>)

將字元範圍寫入目前的資料流,並按照所使用的 Encoding 和可能為寫入資料流的特定字元,將資料流中目前的位置往前移。Writes a span of characters to the current stream, and advances the current position of the stream in accordance with the Encoding used and perhaps the specific characters being written to the stream.

Write(SByte)

將帶正負號 (Signed) 位元組寫入目前資料流,並將資料流位置前移一個位元組。Writes a signed byte to the current stream and advances the stream position by one byte.

Write(Single)

將四位元組浮點數 (Floating-Point) 值寫入目前資料流,並將資料流目前位置前移四個位元組。Writes a four-byte floating-point value to the current stream and advances the stream position by four bytes.

Write(String)

將有長度前置字元的字串以目前 BinaryWriter 的編碼方式寫入此資料流,並按照所使用的編碼方式和寫入資料流的特定字元,將資料流中目前的位置往前移。Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.

Write(UInt16)

將二位元組不帶正負號的整數寫入目前資料流,並將資料流目前位置前移兩個位元組。Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.

Write(UInt32)

將四位元組不帶正負號的整數寫入目前資料流,並將資料流目前位置前移四個位元組。Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes.

Write(UInt64)

將八位元組不帶正負號的整數值寫入目前資料流,並將資料流目前位置前移八個位元組。Writes an eight-byte unsigned integer to the current stream and advances the stream position by eight bytes.

Write7BitEncodedInt(Int32)

以壓縮格式寫入 32 位元整數。Writes a 32-bit integer in a compressed format.

明確介面實作

IDisposable.Dispose()

釋放 BinaryWriter 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the BinaryWriter and optionally releases the managed resources.

適用於

另請參閱