BinaryWriter 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 BinaryWriter 類別的新執行個體,其將寫入資料流。
多載
BinaryWriter() |
初始化 BinaryWriter 類別的新執行個體,其將寫入資料流。 |
BinaryWriter(Stream) |
根據指定的資料流並使用 UTF-8 編碼方式,初始化 BinaryWriter 類別的新執行個體。 |
BinaryWriter(Stream, Encoding) |
根據指定的資料流和字元編碼,初始化 BinaryWriter 類別的新執行個體。 |
BinaryWriter(Stream, Encoding, Boolean) |
根據指定的資料流和特定的字元編碼,初始化 BinaryWriter 類別的新執行個體,並選擇性地保留資料流開啟狀態。 |
BinaryWriter()
初始化 BinaryWriter 類別的新執行個體,其將寫入資料流。
protected:
BinaryWriter();
protected BinaryWriter ();
Protected Sub New ()
備註
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。
另請參閱
適用於
BinaryWriter(Stream)
根據指定的資料流並使用 UTF-8 編碼方式,初始化 BinaryWriter 類別的新執行個體。
public:
BinaryWriter(System::IO::Stream ^ output);
public BinaryWriter (System.IO.Stream output);
new System.IO.BinaryWriter : System.IO.Stream -> System.IO.BinaryWriter
Public Sub New (output As Stream)
參數
- output
- Stream
輸出資料流。
例外狀況
資料流不支援寫入,或已經關閉。
output
為 null
。
範例
下列程式代碼範例示範如何在檔案中儲存和擷取應用程式設定。
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
備註
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。
另請參閱
適用於
BinaryWriter(Stream, Encoding)
根據指定的資料流和字元編碼,初始化 BinaryWriter 類別的新執行個體。
public:
BinaryWriter(System::IO::Stream ^ output, System::Text::Encoding ^ encoding);
public BinaryWriter (System.IO.Stream output, System.Text.Encoding encoding);
new System.IO.BinaryWriter : System.IO.Stream * System.Text.Encoding -> System.IO.BinaryWriter
Public Sub New (output As Stream, encoding As Encoding)
參數
- output
- Stream
輸出資料流。
- encoding
- Encoding
要使用的字元編碼。
例外狀況
資料流不支援寫入,或已經關閉。
output
或 encoding
為 null
。
備註
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。
另請參閱
適用於
BinaryWriter(Stream, Encoding, Boolean)
根據指定的資料流和特定的字元編碼,初始化 BinaryWriter 類別的新執行個體,並選擇性地保留資料流開啟狀態。
public:
BinaryWriter(System::IO::Stream ^ output, System::Text::Encoding ^ encoding, bool leaveOpen);
public BinaryWriter (System.IO.Stream output, System.Text.Encoding encoding, bool leaveOpen);
new System.IO.BinaryWriter : System.IO.Stream * System.Text.Encoding * bool -> System.IO.BinaryWriter
Public Sub New (output As Stream, encoding As Encoding, leaveOpen As Boolean)
參數
- output
- Stream
輸出資料流。
- encoding
- Encoding
要使用的字元編碼。
- leaveOpen
- Boolean
true
表示在處置 BinaryWriter 物件之後,將資料流保持開啟;否則為 false
。
例外狀況
資料流不支援寫入,或已經關閉。
output
或 encoding
為 null
。