Advertisement



< Prev
Next >



C# BinaryReader



In our last tutorial, we have explained how you to use the BinaryWriter class, which is used to write the primitive values like char, int, float, double or even String to a file. In this tutorial, we are going to explain the BinaryReader class, which is used to create a stream, which can be used to read the primitive read the primitive values and String values written by the which have been written by BinaryWriter class.




Some commonly used constructors of BinaryReader


Constructors Description
BinaryReader(System.IO.Stream stream)
This constructor creates an object of the BinaryReader based on the specified stream and using the default UTF-8 encoding.

FileStream(System.IO.Stream stream, System.Text.Encoding encoding)
The constructor created an object of the BinaryReader class based on the specified Stream and character encoding.






Some commonly used write methods of BinaryReader


Methods Description
int ReadInt32()
This method reads a signed int value from the stream.

uint ReadUInt32()
This method reads an unsigned int value from the stream.

char ReadChar()
This method reads a character value from the stream.

short ReadInt16()
This method reads a signed short value from the stream.

ushort ReadUInt16()
This method reads an unsigned short value from the stream.

byte ReadByte()
This method reads an unsigned byte value from the stream.

sbyte ReadSByte()
This method reads an signed byte value from the stream.

float ReadSingle()
This method reads a float from the stream.

double ReadDouble()
This method reads a double from the stream.

long ReadInt64()
This method reads a long from the stream.

String ReadString()
This method reads a String from the stream.

void Close()
This method closes this stream and also frees any resources connected with this stream.






In this example, we are going create an object of the BinaryWriter class and use it to write different types of primitive values and a String object to a file, by using the default UTF-8 encoding. And next, we are going create an object of the BinaryReader class and use it to read the primitive values and String values written by the which have been written by BinaryWriter class by using the default UTF-8 encoding.

//C# Example of BinaryReader and BinaryWriter with default UTF-8 encoding.


using System;
using System.IO;
using System.Text;

class A
{
public static void Main(String[] ar)
{
	A ob = new A();
	ob.WriteValues();
	ob.ReadValues();
}

public void WriteValues()
{
	try
	{
		//Creating variables with primitives values
		int i = -10;
		uint ui = 9;
		char ch = 'C';
		short s = -99;
		ushort us = 99;
		float f = 89.9f;
		double d = 875.888;
		long l = 10000000000;
		
		//Creating a String object
		String str = "Keep smiling and think positive.";
	

		//Creating an object of FileStream to write to a file
		//We are going to use the FileMode OpenOrCreate to create a new file to write to it
		FileStream fs = new FileStream("D:\\File2.txt", FileMode.Create);


		//Creating an object of BinaryWriter to write to a file using FileStream 
		BinaryWriter bw = new BinaryWriter(fs);

		Console.WriteLine("Writing to the file using BinaryStream in memory");

		//writing an int 
		bw.Write(i);

		//writing an uint 
		bw.Write(ui);

		//writing a char
		bw.Write(ch);

		//writing a short
		bw.Write(s);

		//writing a ushort
		bw.Write(us);

		//writing a float 
		bw.Write(f);

		//writing a double
		bw.Write(d);

		//writing a long
		bw.Write(l);

		//writing a String 
		bw.Write(str);
	
	
		//Calling the Flush() method to flush the buffered bytes to the internal array
		bw.Flush();
		
		//Closing the stream i.e. FileStream stream
		fs.Close();

		//Closing the stream i.e. BinaryWriter stream
		bw.Close();
	}
	catch(IOException e)
	{
		Console.WriteLine(e);
	}
}

public void ReadValues()
{
	try 
	{
		//Creating an object of FileStream to read to a file
		//We are going to use the FileMode Open to create a new file to write to it
		FileStream fs = new FileStream("D:\\File2.txt", FileMode.Open);

		//Creating an object of BinaryReader to read from a file using FileStream 
		BinaryReader br = new BinaryReader(fs);

		Console.WriteLine("Reading values from a file using BinaryStream in memory");

		//Reading an int 
		Console.WriteLine(br.ReadInt32());

		//Reading an uint 
		Console.WriteLine(br.ReadUInt32());

		//Reading a char
		Console.WriteLine(br.ReadChar());

		//Reading a short
		Console.WriteLine(br.ReadInt16());

		//Reading a ushort
		Console.WriteLine(br.ReadUInt16());

		//Reading a float 
		Console.WriteLine(br.ReadSingle());

		//Reading a double
		Console.WriteLine(br.ReadDouble());

		//Reading a long
		Console.WriteLine(br.ReadInt64());

		//Reading a String 
		Console.WriteLine(br.ReadString());
		

		//Closing the stream i.e. FileStream stream
		fs.Close();

		//Closing the stream i.e. BinaryReader stream
		br.Close();
	}
	catch(IOException e)
	{
		Console.WriteLine(e);
	}	
}
}


Output is -:


Writing to the file using BinaryStream in memory
Reading values from a file using BinaryStream in memory
-10
9
C
-99
99
89.9
875.888
10000000000
Keep smiling and think positive.




Note: To make it a successful reading of data, the data should be read in the same order in which it was written, as we have shown in the example. .


Advertisement




  • Example of BinaryReader with a non-default Unicode encoding.


  • In this example, we are going create an object of the BinaryWriter class and use it to write different types of primitive values and a String object to a file, by using the non-default Unicode encoding. And next, we are going create an object of the BinaryReader class and use it to read the primitive values and String values written by the which have been written by BinaryWriter class by using the non-default Unicode encoding.

    //C# Example of BinaryReader and BinaryWriter with default UTF-8 encoding.
    
    
    using System;
    using System.IO;
    using System.Text;
    
    class A
    {
    public static void Main(String[] ar)
    {
    	A ob = new A();
    	ob.WriteValues();
    	ob.ReadValues();
    }
    
    public void WriteValues()
    {
    	try
    	{
    		//Creating variables with primitives values
    		int i = -10;
    		uint ui = 9;
    		char ch = 'C';
    		short s = -99;
    		ushort us = 99;
    		float f = 89.9f;
    		double d = 875.888;
    		long l = 10000000000;
    		
    		//Creating a String object
    		String str = "Keep smiling and think positive.";
    	
    		//Creating an object of Encoding type
    		Encoding unicode = Encoding.Unicode;
    
    		//Creating an object of FileStream to write to a file
    		//We are going to use the FileMode OpenOrCreate to create a new file to write to it
    		FileStream fs = new FileStream("D:\\File6.txt", FileMode.Create);
    
    
    		//Creating an object of BinaryWriter to write to a file using FileStream 
    		BinaryWriter bw = new BinaryWriter(fs, unicode);
    
    		Console.WriteLine("Writing to the file using BinaryStream in memory");
    
    		//writing an int 
    		bw.Write(i);
    
    		//writing an uint 
    		bw.Write(ui);
    
    		//writing a char
    		bw.Write(ch);
    
    		//writing a short
    		bw.Write(s);
    
    		//writing a ushort
    		bw.Write(us);
    
    		//writing a float 
    		bw.Write(f);
    
    		//writing a double
    		bw.Write(d);
    
    		//writing a long
    		bw.Write(l);
    
    		//writing a String 
    		bw.Write(str);
    	
    	
    		//Calling the Flush() method to flush the buffered bytes to the internal array
    		bw.Flush();
    		
    		//Closing the stream i.e. FileStream stream
    		fs.Close();
    
    		//Closing the stream i.e. BinaryWriter stream
    		bw.Close();
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}
    }
    
    public void ReadValues()
    {
    	try 
    	{
    		//Creating an object of Encoding type
    		Encoding unicode = Encoding.Unicode;
    		
    
    		//Creating an object of FileStream to read to a file
    		//We are going to use the FileMode OpenOrCreate to create a new file to write to it
    		FileStream fs = new FileStream("D:\\File6.txt", FileMode.Open);
    
    		//Creating an object of BinaryReader to read from a file using FileStream 
    		BinaryReader br = new BinaryReader(fs, unicode);
    
    		Console.WriteLine("Reading values from a file using BinaryStream in memory");
    
    		//Reading an int 
    		Console.WriteLine(br.ReadInt32());
    
    		//Reading an uint 
    		Console.WriteLine(br.ReadUInt32());
    
    		//Reading a char
    		Console.WriteLine(br.ReadChar());
    
    		//Reading a short
    		Console.WriteLine(br.ReadInt16());
    
    		//Reading a ushort
    		Console.WriteLine(br.ReadUInt16());
    
    		//Reading a float 
    		Console.WriteLine(br.ReadSingle());
    
    		//Reading a double
    		Console.WriteLine(br.ReadDouble());
    
    		//Reading a long
    		Console.WriteLine(br.ReadInt64());
    
    		//Reading a String 
    		Console.WriteLine(br.ReadString());
    		
    
    		//Closing the stream i.e. FileStream stream
    		fs.Close();
    
    		//Closing the stream i.e. BinaryReader stream
    		br.Close();
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}	
    }
    }
    


    Output is -:


    Writing to the file using BinaryStream in memory
    Reading values from a file using BinaryStream in memory
    -10
    9
    C
    -99
    99
    89.9
    875.888
    10000000000
    Keep smiling and think positive.
    







    Please share this article -




    < Prev
    Next >
    < C# BinaryWriter
    C# File >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement