Open In App

C# Binary Writer

Last Updated : 04 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisites: Introduction to C#

C# is a general-purpose, modern and object-oriented programming language pronounced as “C Sharp”. It was developed by Microsoft led by Anders Hejlsberg. In C# Binary Writer is a class that is used to write primitive types as binary data in particular encoding stream. It is present under the System.IO namespace.

public class BinaryWriter : IAsyncDisposable, IDisposable
Binary writer class implements
 IAsyncDisposable and IDisposable interface

Syntax: 

initialize an instance of binary writer class

BinaryWriter bw = new BinaryWriter()

Initializes an instance of the BinaryWriter class based on the specified stream and using UTF-8 encoding.

BinaryWriter bw = new BinaryWriter(stream)

This will take a specified stream and any character encoding according to need.

BinaryWriter bw = new BinaryWriter(stream,encoding)

This will take a specified stream, character encoding, and an optional boolean value. If the boolean value is true then it will leave the output stream open after the disposal of the BinaryWriter object.

BinaryWriter bw = new BinaryWriter(stream,encoding,true)
S.No.

Method

Description

1

Write(Boolean)

Writes a one-byte boolean value 

2

Write(Byte)

Writes an unsigned byte to the current stream and advances 

the stream position by one byte

3

Write(Char)

Writes an Unicode character to the current stream and advances the

current position of the stream according to the Encoding

used and the character that has been written.

4

Write(Double)

Writes an eight-byte floating-point value to the current stream and 

advances the stream position by eight bytes.

5

Write(Int32)

Writes a four-byte unsigned integer to the current stream and 

advances the stream position by four bytes.

6

Write(String)

Writes a particular string to the current stream and advances the

current position of the stream according to the Encoding

used and the characters that have been there in string.

Example:

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:\test\test.txt", FileMode.Create)))
            {
                //writes the data to the stream
                bw.Write(25);
                bw.Write(23.98);
                bw.Write('c');
                bw.Write("GeeksForGeeks");
                bw.Write(true);
                Console.WriteLine("Successfully Written");
                Console.ReadLine();
            }
        }
    }
}


Output:

Successfully Written

Now we see how to retrieve the data from the file.

Example 2: 

C#




// C# program for how to retrieve
// the data from the file:
 
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 (BinaryReader bw = new BinaryReader
                   (File.Open(@"C:\test\test.txt", FileMode.Open)))
            {
                //Reads the data to the stream
                Console.WriteLine("String value is " + bw.ReadInt32());
                Console.WriteLine("Double value is " + bw.ReadDouble());
                Console.WriteLine("Char value is " + bw.ReadChar());
                Console.WriteLine("value of string is " + bw.ReadString());
                Console.WriteLine("for boolean value is " + bw.ReadBoolean());
                Console.Read();
                
            }
        }
    }
}


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads