Open In App

How to Use C# BinaryWriter Class?

In this article, we will illustrate a binary writer in C#. Following are some important points regarding Binary Writer:

Syntax:



The syntax to create a Binary Writer object using constructors is given below:

protected BinaryWriter();



The below statement computes a new instance of the Binary Writer class and by default, it uses UTF-8 character encoding.

BinaryWriter binaryWriter = new BinaryWriter(outputStream) ;

The below statement computes a new instance of the Binary Writer class and character encoding.

BinaryWriter binaryWriter = new BinaryWriter(outputStream, encoding);

Working of Binary Writer Works in C#:

Methods of Binary Writer:

Below are some methods of Binary Writer in C#:

Method Description
Write (Boolean)

This method is used to represent the one-byte Boolean value to the present stream. 

0 stands for false and 1 for true.

Write (Byte) This method is used to represent the unsigned byte to the present stream.
Write (Char)

This method is used to represent Unicode characters in the present stream.

And it also proceeds the current stream position based on character encrypting used.

Write (Decimal)

This method is used to represent a decimal value to the present stream.

It proceeds the position of the current stream by sixteen bytes.

Write (Double)

This method is used to represent an eight-byte floating value to the present stream.

It proceeds the position of the current stream by eight bytes.

Write (Int32)

This method is used to represent a four-byte signed integer to the present stream.

It proceeds the position of the current stream by four bytes.

Write (String)

This method is used to represent length prefixed string to present stream in the present encrypting of Binary Writer.

It also proceeds the current stream position based on character encrypting used.

Now, here is one method called write which basically writes a Boolean value to the stream as a one-byte value.

Example 1:




using System;
using System.IO;
class binary
{
    const string fileName = "hello.dat";
    static void Main()
    {
       Write();
       Console.WriteLine("Binary Writer");
    }
    public static void Write()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, 
                                                      FileMode.Create)))
        {
            writer.Write(helloworld);
            writer.Write(@"c:\Temp");
        }
    }
}

Output:

 

Code Explanation:

This C# program is used to illustrate binary writer. With the help of the binary writer class, the contents are written in the file. It provides a way that makes it easier to write primitive data types to a stream. The write() method writes a Boolean value to the stream as a one-byte value.

Example 2:




// C# program for binary writer
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
   
namespace binary_writer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (BinaryWriter bw = new BinaryWriter(File.Open(
              @"C:\New\First.txt", FileMode.Create)))
            {
                //writes the data to the stream
                bw.Write('Hello World');
                bw.Write('Welcome');
                bw.Write('c#');
                bw.Write("GeeksForGeeks");
                bw.Write(true);
                Console.WriteLine("Successfully Added");
                Console.ReadLine();
            }
        }
    }
}

Output:

Successfully Added

Article Tags :
C#