Implementing Binary Reader Using C#

Last Updated : 26 Sep, 2025

BinaryReader is a class in the System.IO namespace that is used to read primitive data types in binary format from streams. It supports data types such as int, double, string, boolean and others and is typically used together with BinaryWriter for writing and reading binary data.

Syntax

This will create the BinaryReader object for a particular input stream by using UTF-8 encoding.

BinaryReader br = new BinaryReader(Stream)

This will take a particular input stream and can pass any character encoding according to need.

BinaryReader br = new BinaryReader(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 stream open after the disposal of the BinaryReader object.

BinaryReader br = new BinaryReader(Stream,Encoding,True)

Methods

MethodDescription
Read()Reads characters from the current stream and advances the position based on encoding.
ReadByte()Reads the next byte and advances the position by one byte.
ReadBoolean()Reads a Boolean value and advances the position by one byte.
ReadDouble()Reads an 8-byte floating-point value and advances the position by eight bytes.
ReadInt32()Reads a 4-byte signed integer and advances the position by four bytes.
ReadString()Reads a string from the current stream.
ReadChar()Reads the next character and advances the position based on encoding.
ReadChars(int count)Reads the specified number of characters into a character array and advances the position based on encoding.

Example

C#
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)
        {

            string filePath = @"C:\test\test.txt";

            //write in the stream
            using (BinaryWriter bw = new BinaryWriter(File.Open(filePath,
                                    FileMode.Create), Encoding.UTF8, false))
            {
                bw.Write(76);
                bw.Write(1.2);
                bw.Write('a');
                bw.Write("GeeksForGeeks");
                bw.Write(false);
            }
            if (File.Exists(filePath))
            {
                using (BinaryReader br = new BinaryReader(File.Open(filePath, 
                                         FileMode.Open), Encoding.UTF8))
                {
                    //Reads the data to the stream
                    Console.WriteLine("int value is " + br.ReadInt32());
                    Console.WriteLine("Double value is " + br.ReadDouble());
                    Console.WriteLine("Char value is " + br.ReadChar());
                    Console.WriteLine("value of string is " + br.ReadString());
                    Console.WriteLine("for boolean value is " + br.ReadBoolean());
                    Console.Read();

                }

            }
           
        }
    }
}

Output:

Output
Comment

Explore