Reading and Writing Binary Data

To read and write binary values of the C# built-in types, you will use BinaryReader and BinaryWriter. When using these streams, it is important
to understand that this data is read and written using its internal, binary format, not its human-readable text form.

A BinaryWriter is a wrapper around a byte stream that manages the writing of binary data.
Its most commonly used constructor is shown here:
BinaryWriter(Stream output)
Here, output is the stream to which data is written. To write output to a file, you can use the object created by FileStream for this parameter. If output is null, then an ArgumentNullException is thrown. If output has not been opened for writing, ArgumentException is thrown. When you are done using a BinaryWriter, you must close it. Closing a BinaryWriter also closes the underlying stream.

BinaryReader

A BinaryReader is a wrapper around a byte stream that handles the reading of binary data. Its most commonly used constructor is shown here:
BinaryReader(Stream input)
Here, input is the stream from which data is read. To read from a file, you can use the object created by FileStream for this parameter. If input has not been opened for reading or is otherwise invalid, ArgumentException is thrown. When you are done with a BinaryReader you must close it. Closing a BinaryReader also closes the underlying stream.
Here is a program that demonstrates BinaryReader and BinaryWriter. It writes and then reads back various types of data to and from a file.

Example

using System;
using System.IO;
namespace ConsoleApplication1
{
    class RWData
    {
        static void Main()
        {
            BinaryWriter dataOut;
            BinaryReader dataIn;
            int i = 10;
            double d = 1023.56;
            bool b = true;
            string str = "This is a test";
            // Open the file for output.
            try
            {
                dataOut = new
                BinaryWriter(new FileStream("testdata", FileMode.Create));
            }
            catch (IOException exc)
            {
                Console.WriteLine("Error Opening File:\n" + exc.Message);
                return;
            }
            // Write data to a file.
            try
            {
                Console.WriteLine("Writing " + i);
                dataOut.Write(i);
                Console.WriteLine("Writing " + d);
                dataOut.Write(d);
                Console.WriteLine("Writing " + b);
                dataOut.Write(b);
                Console.WriteLine("Writing " + 12.2 * 7.4);
                dataOut.Write(12.2 * 7.4);
                Console.WriteLine("Writing " + str);
                dataOut.Write(str);
            }
            catch (IOException exc)
            {
                Console.WriteLine("I/O Error:\n" + exc.Message);
            }
            finally
            {
                dataOut.Close();
            }
            Console.WriteLine();
            // Now, read the data.
            try
            {
                dataIn = new
                BinaryReader(new FileStream("testdata", FileMode.Open));
            }
            catch (IOException exc)
            {
                Console.WriteLine("Error Opening File:\n" + exc.Message);
                return;
            }
            try
            {
                i = dataIn.ReadInt32();
                Console.WriteLine("Reading " + i);
                d = dataIn.ReadDouble();
                Console.WriteLine("Reading " + d);
                b = dataIn.ReadBoolean();
                Console.WriteLine("Reading " + b);
                d = dataIn.ReadDouble();
                Console.WriteLine("Reading " + d);
                str = dataIn.ReadString();
                Console.WriteLine("Reading " + str);
            }
            catch (IOException exc)
            {
                Console.WriteLine("I/O Error:\n" + exc.Message);
            }
            finally
            {
                dataIn.Close();
            }
        }
    }
}