Saturday 28 May 2016

binaryreader

Read binary file.
It is used to read primitive data types as binary values in a specific encoding.
It does not contain default constructor but you can use parameter constructor.
List of parameter constructor:

public BinaryReader(Stream input);
Initializes a new instance of the BinaryReader class based on the specified stream and using UTF-8 encoding.
Stream: input is name of the input stream.

public BinaryReader(Stream input, Encoding encoding);
Initializes a new instance of the BinaryReader class based on the specified stream and character encoding.
Encoding: is the character encoding to use. Encoding is available in System.Text namespace.  Encoding is an abstract class. It is used to various text formats.

public BinaryReader(Stream input, Encoding encoding, bool leaveOpen);
Initializes a new instance of the BinaryReader class based on the specified stream and character encoding, and optionally leaves the stream.
leaveOpen: True to leave the stream open after the BinaryReader object is disposed; otherwise, false.
Take an example of binary reader:

using System;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        try
        {
            using (BinaryReader br = new BinaryReader(File.Open("test.txt", FileMode.Open)))
            {
                int position = 0;

                int length = (int)br.BaseStream.Length;

                while (position < length)
                {
                    int a = br.ReadInt32();
                    Console.WriteLine(a);
                    position += sizeof(int);
                }
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }
    }
}
In above example using binary reader open “test.txt” file which is created by binary writer and then one by one read binary stream from test file and added console.
Sizeof(int) use for jump to the next position. Here we used int and int sizeof is 4.
List of methods in BinaryReader:
Read: Reads character from the underlying stream. You can also use Byte[], Char[], Int32;
ReadUInt64: Reads an 8 byte unsigned integer from the current underlying stream.
ReadUInt32: Reads a 4 byte unsigned integer from the current underlying stream.
ReadUInt16: Reads a 2 byte unsigned integer from the current underlying stream.
ReadString: Reads a string from the current underlying stream.
ReadSingle: Reads a a 4 byte floating point value from the current underlying stream.
ReadSByte: Reads a signed byte from the current underlying stream.
ReadInt32: Reads a 4 byte signed integer from the current underlying stream.
ReadInt16: Reads a 2 byte signed integer from the current underlying stream.
ReadDobule: Reads a 8 byte floating point value from the current underlying stream.
ReadDecimal: Reads a decimal value from the current underlying stream.
ReadChar: Reads the next character from the current stream and advaces the current position of the stream.
ReadChars(int32): Reads the specified number of character from the current stream.
ReadByte: Read the next byte from the current stream and advance the current position of the stream.
ReadBytes(int32): Reads the specified number of bytes from the current stream into a byte array.
ReadBoolean: Read a Boolean value from the current underlying stream.
Read7BitEncodedInt: Reads in a 32 bit integer in compressed format.
Close: Close the current reader and the underlying stream.
Dispose: Release all resource used by the current instance of the binary reader class.
Finalize: Allows an object to try to free resources and perform other cleanup operations before it is reclaimed garbage collection.
PeekChar: Return the next available character and does not advance the byte or character position.

you can also visit:  www.mybook-lang.com

No comments:

Post a Comment