Open In App

How to Use C# BinaryWriter Class?

Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Binary Writer is used to generating binary files.
  • Strings can be written in specific encoding using Binary Writer.
  • Binary Writer constructor takes an object of type Stream to create an object for the Binary Writer class.
  • The encoding is not specified and remains UTF-8 by default.

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#:

  • In C#, Binary Writer is used to writing some binary data to a file or it is used to produce binary files. It helps us write primitive data types in double format to a stream. It also helps us write strings in a particular character encrypting.
  • When we are working with Binary Writer, it is important to include the System.IO namespace in the program. Now, we can easily create the object of the binary writer with the help of the ‘new’ operator and also pass a stream object to the constructor.
  • To produce an instance of Binary Writer, we usually provide a stream object to its constructor and at the same time, we can provide an optional parameter that states the encrypting to be used while writing the file. The encoding is not specified and remains UTF-8 by default.
  • Now, there is one more optional parameter that can be passed to the constructor while creating the object of Binary Writer. It is of type Boolean and it is used to identify whether the person wants the current stream to remain open or not.

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:

C#




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#




// 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


Last Updated : 24 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads