C# BinaryWriter BinaryWriter(Stream, Encoding)

Description

BinaryWriter BinaryWriter(Stream, Encoding) Initializes a new instance of the BinaryWriter class based on the specified stream and character encoding.

Syntax

BinaryWriter.BinaryWriter(Stream, Encoding) has the following syntax.


public BinaryWriter(
  Stream output,
  Encoding encoding
)

Parameters

BinaryWriter.BinaryWriter(Stream, Encoding) has the following parameters.

  • output - The output stream.
  • encoding - The character encoding to use.

Example


//from   w w w.ja v  a 2 s.  co  m
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),System.Text.Encoding.ASCII))
        {
            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