Skip to content
Related Articles

Related Articles

Implementing Binary Reader Using C#

View Discussion
Improve Article
Save Article
  • Last Updated : 19 Jul, 2022
View Discussion
Improve Article
Save Article

BinaryReader is a class that is present under the System.IO namespace. This is used for handling the binary values from a particular encoding stream.

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:

S.No.

Method

Description

1Read()

The Read will read the characters in the current stream and advances 

the current position according to the encoding.

2ReadByte()

Read the next byte from the current stream and advances 

the current position by one byte.

3ReadBoolean()

Read boolean from current stream and advances

the current position of the stream by one byte.

4ReadDouble()

Read 8 byte floating point value from current stream and

advances the current position of the stream by eight bytes.

5ReadInt32()

Read 4 byte signed integer value from current stream and

advances the  current position of the stream by four bytes.

6ReadStringRead the string from the current stream
7ReadChar()

Reads the next character from the current stream and advances

the current position of the stream according to the Encoding used 

8ReadChars(Int32)

Reads the specified number of characters from the current 

stream returns the data in a character array, and advances the current

 position according to the Encoding used and the number of characters 

being read from a stream.

Example: 

C#




// C# Program for Implementing BinaryReader
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:

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!