Bagikan melalui


BinaryWriter Konstruktor

Definisi

Menginisialisasi instans BinaryWriter baru kelas yang menulis ke aliran.

Overload

BinaryWriter()

Menginisialisasi instans BinaryWriter baru kelas yang menulis ke aliran.

BinaryWriter(Stream)

Menginisialisasi instans BinaryWriter baru kelas berdasarkan aliran yang ditentukan dan menggunakan pengodean UTF-8.

BinaryWriter(Stream, Encoding)

Menginisialisasi instans BinaryWriter baru kelas berdasarkan aliran dan pengodean karakter yang ditentukan.

BinaryWriter(Stream, Encoding, Boolean)

Menginisialisasi instans BinaryWriter baru kelas berdasarkan aliran dan pengodean karakter yang ditentukan, dan secara opsional membiarkan aliran terbuka.

BinaryWriter()

Sumber:
BinaryWriter.cs
Sumber:
BinaryWriter.cs
Sumber:
BinaryWriter.cs

Menginisialisasi instans BinaryWriter baru kelas yang menulis ke aliran.

protected:
 BinaryWriter();
protected BinaryWriter ();
Protected Sub New ()

Keterangan

Untuk daftar tugas I/O umum, lihat Tugas I/O Umum.

Lihat juga

Berlaku untuk

BinaryWriter(Stream)

Sumber:
BinaryWriter.cs
Sumber:
BinaryWriter.cs
Sumber:
BinaryWriter.cs

Menginisialisasi instans BinaryWriter baru kelas berdasarkan aliran yang ditentukan dan menggunakan pengodean UTF-8.

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)

Parameter

output
Stream

Aliran output.

Pengecualian

Aliran tidak mendukung penulisan atau sudah ditutup.

outputadalah null.

Contoh

Contoh kode berikut menunjukkan cara menyimpan dan mengambil pengaturan aplikasi dalam file.

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

Keterangan

Untuk daftar tugas I/O umum, lihat Tugas I/O Umum.

Lihat juga

Berlaku untuk

BinaryWriter(Stream, Encoding)

Sumber:
BinaryWriter.cs
Sumber:
BinaryWriter.cs
Sumber:
BinaryWriter.cs

Menginisialisasi instans BinaryWriter baru kelas berdasarkan aliran dan pengodean karakter yang ditentukan.

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)

Parameter

output
Stream

Aliran output.

encoding
Encoding

Pengodean karakter yang akan digunakan.

Pengecualian

Aliran tidak mendukung penulisan atau sudah ditutup.

output atau encoding adalah null.

Keterangan

Untuk daftar tugas I/O umum, lihat Tugas I/O Umum.

Lihat juga

Berlaku untuk

BinaryWriter(Stream, Encoding, Boolean)

Sumber:
BinaryWriter.cs
Sumber:
BinaryWriter.cs
Sumber:
BinaryWriter.cs

Menginisialisasi instans BinaryWriter baru kelas berdasarkan aliran dan pengodean karakter yang ditentukan, dan secara opsional membiarkan aliran terbuka.

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)

Parameter

output
Stream

Aliran output.

encoding
Encoding

Pengodean karakter yang akan digunakan.

leaveOpen
Boolean

true untuk membiarkan aliran terbuka setelah BinaryWriter objek dibuang; jika tidak, false.

Pengecualian

Aliran tidak mendukung penulisan atau sudah ditutup.

output atau encoding adalah null.

Lihat juga

Berlaku untuk