C# Program to Demonstrate Binary Writer
Posted by Superadmin on August 13 2022 08:01:43

C# Program to Demonstrate Binary Writer

 

 

This is a C# Program to illustrate binary writer.

Problem Description

This C# Program Illustrates Binary Writer.

Problem Solution

Here Write method writes a Boolean value to the stream as a one-byte value.

Program/Source Code

Here is source code of the C# Program to Illustrate Binary Writer. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Illustrate Binary Writer
 */
using System;
using System.IO;
class ConsoleApplication
{
    const string fileName = "program.dat";
    static void Main()
    {
       Write();
       Console.WriteLine("Using Binary Writer Class the Contents are Written ");
    }
    public static void Write()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, 
                                                      FileMode.Create)))
        {
            writer.Write(1.250F);
            writer.Write(@"c:\Temp");
        }
    }
}
Program Explanation

This C# program is used to illustrate binary writer. Using binary writer class the contents are written in the file. The BinaryWriter class provides methods that simplify writing primitive data types to a stream. The write() method is used to write a Boolean value to the stream as a one-byte value.

 

The class includes write methods that support different data types while creating a new instance of the BinaryWriter class. Provide the stream to write to, and optionally specify the type of encoding and whether to leave the stream open after disposing the BinaryWriter object. If the encoding type didn’t specified, UTF-8 is used.

Runtime Test Cases
 
Using Binary Writer Class the Contents are Written