BinaryWriter 类

定义

将二进制中的基元类型写入流并支持用特定的编码写入字符串。Writes primitive types in binary to a stream and supports writing strings in a specific encoding.

public ref class BinaryWriter : IDisposable
public class BinaryWriter : IDisposable
[System.Serializable]
public 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. 若要直接释放类型,请在 Disposetry/ 块中调用其 catch 方法。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 使用的非托管资源,并选择性地释放托管资源。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)

将一个无符号字节写入当前流,并将流的位置提升 1 个字节。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)

将 8 字节浮点值写入当前流,并将流的位置提升 8 个字节。Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.

Write(Int16)

将 2 字节带符号整数写入当前流,并将流的位置提升 2 个字节。Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.

Write(Int32)

将 4 字节带符号整数写入当前流,并将流的位置提升 4 个字节。Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.

Write(Int64)

将 8 字节带符号整数写入当前流,并将流的位置提升 8 个字节。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)

将一个带符号字节写入当前流,并将流的位置提升 1 个字节。Writes a signed byte to the current stream and advances the stream position by one byte.

Write(Single)

将 4 字节浮点值写入当前流,并将流的位置提升 4 个字节。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)

将 2 字节无符号整数写入当前流,并将流的位置提升 2 个字节。Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.

Write(UInt32)

将 4 字节无符号整数写入当前流,并将流的位置提升 4 个字节。Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes.

Write(UInt64)

将 8 字节无符号整数写入当前流,并将流的位置提升 8 个字节。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 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the BinaryWriter and optionally releases the managed resources.

适用于

另请参阅