Topic : Reading from and Writing into Binary Files - BinaryReader Class | BinaryWriter Class-



BinaryReader Class:

The BinaryReader class is used to read binary data from a file. if We have .bin file stored in our PC and we want to read them, the BinaryReader may help us lot. A Binary file stored data in the different layout that is more Efficient for a machine but not convenient for the human. BinaryReader makes this job simpler and shows us exact data stored in the bin file.

BinaryReader Class provides some methods, that simplify reading primitive data types from a stream. The following table describes commonly used Methods of the BinaryReader Class.



MethodDescription
Close()It closes the BinaryReader object and the underlying stream.
Read()Reads the characters from the underlying stream and advances the current position of the stream.
ReadBoolean()Reads a Boolean value from the current stream and advances the current position of the stream by one Byte.
ReadByte()Reads the next byte from the current stream and advances the current position of the stream by one byte.
ReadChar()Reads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.
ReadDouble()Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
ReadInt32()Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
ReadString()Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.
Dispose()Releases all resources used by the current instance of the BinaryReader class.
Equals(Object)Determines whether the specified object is equal to the current object.
Finalize()Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
PeekChar()Returns the next available character and does not advance the byte or character position.


BinaryWriter Class:

The BinaryWriter Class is used to write binary data to a stream. C# BinaryWriter Object works with stream Objects that provide access to the underlying bytes. For creating a BinaryWriter Object, we have to first create a FileStream Object and then pass BinaryWriter to the Constructor Method.

BinaryWriter The class provides some methods that simplify writing primitive data types to a stream. The Following table describes commonly used methods of the BinaryWriter Class.



MethodsDescription
Close()It closes the BinaryWriter object and the underlying stream.
Flush()Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
Seek()Sets the position within the current stream.
Write(bool value)Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.
Dispose()Releases all resources used by the current instance of the BinaryWriter class.
Finalize()Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
GetType()Gets the Type of the current instance.
ToString()Returns a string that represents the current object.
Write(char)Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.
Write(Decimal)Writes a decimal value to the current stream and advances the stream position by sixteen bytes.
Write(String)Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.
Write(Single)Writes a four-byte floating-point value to the current stream and advances the stream position by four bytes.
Write(Double)Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.


The Following Example show how to store and display Data Using Binary Files:


/*Example - BinaryReader & BinaryWriter Class - InfoBrother*/

using System;
using System.IO;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()   
    {
        WriteDefaultValues();  //Method Called to Write Values:
        DisplayValues();      //Method Called to Display Values:
    }

    public static void WriteDefaultValues() //BinaryWriter
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write(1.250F);
            writer.Write(@"c:\Temp");
            writer.Write(10);
            writer.Write(true);
        }
    }

    public static void DisplayValues()  //BinaryReader
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;
        if (File.Exists(fileName))
        {
            using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                aspectRatio = reader.ReadSingle();
                tempDirectory = reader.ReadString();
                autoSaveTime = reader.ReadInt32();
                showStatusBar = reader.ReadBoolean();
            }

            Console.WriteLine("Aspect ratio set to: " + aspectRatio);
            Console.WriteLine("Temp directory is: " + tempDirectory);
            Console.WriteLine("Auto save time set to: " + autoSaveTime);
            Console.WriteLine("Show status bar: " + showStatusBar);

            Console.ReadKey();
        }
    }
}


NOTE: For complete list of properties and methods, please visit Microsoft's C# documentation BinaryWriter and BinaryReader class.


















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement