C Sharp BinaryWriter

C# BinaryWriter

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

Example:

using System;  
using System.IO;  
namespace Example  
{  
    class content  
    {  
        static void Main(string[] args)  
        {  
            string fileName = "d\\bfile.dat";  
            using (BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Create)))  
            {  
                bw.Write(true);  
                bw.Write("Hello World");  
                bw.Write(1);  
            }  
            Console.WriteLine("Success!!");    
        }  
    }  
}

Console Output:

D\\bfile.dat:

Explanation:

In the above example, we are using the BinaryWriter class to write data into a .dat file.