C# BinaryWriter BinaryWriter(Stream)

Description

BinaryWriter BinaryWriter(Stream) Initializes a new instance of the BinaryWriter class based on the specified stream and using UTF-8 encoding.

Syntax

BinaryWriter.BinaryWriter(Stream) has the following syntax.


public BinaryWriter(
  Stream output
)

Parameters

BinaryWriter.BinaryWriter(Stream) has the following parameters.

  • output - The output stream.

Example

The following code example demonstrates how to store and retrieve application settings in a file.


//from   w  ww  .  j a va  2 s  .c om
using System;
using System.IO;

class ConsoleApplication
{
    const string fileName = "data.dat";

    static void Main()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write(1.250F);
            writer.Write("java2s.com");
            writer.Write(10);
            writer.Write(true);
        }
        using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
        {
            Console.WriteLine(reader.ReadSingle());
            Console.WriteLine(reader.ReadString());
            Console.WriteLine(reader.ReadInt32());
            Console.WriteLine(reader.ReadBoolean());
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter