BinaryWriter 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化写入流的 BinaryWriter 类的新实例。
重载
BinaryWriter() |
初始化写入流的 BinaryWriter 类的新实例。 |
BinaryWriter(Stream) |
基于所指定的流和特定的 UTF-8 编码,初始化 BinaryWriter 类的新实例。 |
BinaryWriter(Stream, Encoding) |
基于所指定的流和特定的字符编码,初始化 BinaryWriter 类的新实例。 |
BinaryWriter(Stream, Encoding, Boolean) |
基于所提供的流和特定的字符编码,初始化 BinaryWriter 类的新实例,有选择性的打开流。 |
BinaryWriter()
- Source:
- BinaryWriter.cs
- Source:
- BinaryWriter.cs
- Source:
- BinaryWriter.cs
初始化写入流的 BinaryWriter 类的新实例。
protected:
BinaryWriter();
protected BinaryWriter ();
Protected Sub New ()
注解
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
BinaryWriter(Stream)
- Source:
- BinaryWriter.cs
- Source:
- BinaryWriter.cs
- Source:
- BinaryWriter.cs
基于所指定的流和特定的 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)
- Source:
- BinaryWriter.cs
- Source:
- BinaryWriter.cs
- Source:
- BinaryWriter.cs
基于所指定的流和特定的字符编码,初始化 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)
- Source:
- BinaryWriter.cs
- Source:
- BinaryWriter.cs
- Source:
- BinaryWriter.cs
基于所提供的流和特定的字符编码,初始化 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
如果在释放 BinaryWriter 对象后保持流处于打开状态,则为 true
;否则为 false
。
例外
该流不支持写入或者该流已关闭。
output
或 encoding
为 null
。