C Sharp BinaryReader

C# BinaryReader

Found in System.IO namespace, the C# BinaryReader class supports specific encoding for reading a string and is thus used for reading the binary information from a stream.

Example:

using System;  
using System.IO;  
namespace Example  
{  
    class content  
    {  
        static void Main(string[] args)  
        {  
            WriteFile();  
            ReadFile();  
            Console.ReadKey();  
        }  
        static void WriteFile()  
        {  
           using (BinaryWriter bw = new BinaryWriter(File.Open("d:\\bfile.dat", FileMode.Create)))  
            {  
 
                bw.Write(true);  
                bw.Write("Hello World");  
                bw.Write(20.2); 
 
            }  
        }  
        static void ReadFile()  
        {  
            using (BinaryReader br = new BinaryReader(File.Open("d:\\bfile.dat", FileMode.Open)))  
            {  
                Console.WriteLine("Boolean Value : " + br.ReadBoolean()); 
                Console.WriteLine("String Value : " + br.ReadString());  
                Console.WriteLine("Double Value : " + br.ReadDouble());  
            }  
        }  
    }  
}

Output:

Explanation:

In the above example, we are using the BinaryReader class to read data from a .dat file.

Please Share