BinaryReader Class
Provides a means to read primitive data types from a stream in binary format.
Syntax
public class BinaryReader : IDisposable
Remarks
The BinaryReader class supports reading various primitive data types (like boolean, char, byte, short, int, long, float, and double) from a stream. It also supports reading strings. The data can be written to a stream using the BinaryWriter class.
When you create an instance of BinaryReader, you must provide a stream that is readable. The BinaryReader is not thread-safe. If you need to perform operations on the stream from multiple threads, you must synchronize access to the stream.
BinaryReader reads data based on the encoding specified when the BinaryReader is instantiated. If no encoding is specified, the default is UTF-8.
Constructors
-
BinaryReader(Stream input)
public BinaryReader(Stream input)
Initializes a new instance of the
BinaryReaderclass using UTF-8 encoding and a buffer size of 4096 bytes. -
BinaryReader(Stream input, Encoding encoding)
public BinaryReader(Stream input, Encoding encoding)
Initializes a new instance of the
BinaryReaderclass using the specified encoding and a buffer size of 4096 bytes. -
BinaryReader(Stream input, Encoding encoding, bool leaveOpen)
public BinaryReader(Stream input, Encoding encoding, bool leaveOpen)
Initializes a new instance of the
BinaryReaderclass using the specified encoding and optionally leaves the stream open after theBinaryReaderobject is disposed.
Methods
-
ReadBoolean()
public virtual bool ReadBoolean()
Reads a Boolean value from the current stream and advances the position in the stream by one byte.
-
ReadByte()
public virtual byte ReadByte()
Reads a byte from the current stream and advances the position in the stream by one byte.
-
ReadBytes(int count)
public virtual byte[] ReadBytes(int count)
Reads the specified number of bytes from the current stream and advances the position in the stream by that number of bytes.
-
ReadChar()
public virtual char ReadChar()
Reads the next character from the current stream and advances the current position accordingly.
-
ReadChars(int count)
public virtual char[] ReadChars(int count)
Reads the specified number of characters from the current stream and advances the current position accordingly.
-
ReadDecimal()
public virtual decimal ReadDecimal()
Reads a decimal value from the current stream and advances the position in the stream by sixteen bytes.
-
ReadDouble()
public virtual double ReadDouble()
Reads an 8-byte floating-point value from the current stream and advances the position in the stream by eight bytes.
-
ReadInt16()
public virtual short ReadInt16()
Reads a 2-byte signed integer from the current stream and advances the position in the stream by two bytes.
-
ReadInt32()
public virtual int ReadInt32()
Reads a 4-byte signed integer from the current stream and advances the position in the stream by four bytes.
-
ReadInt64()
public virtual long ReadInt64()
Reads an 8-byte signed integer from the current stream and advances the position in the stream by eight bytes.
-
ReadSByte()
public virtual sbyte ReadSByte()
Reads a signed byte from the current stream and advances the position in the stream by one byte.
-
ReadSingle()
public virtual float ReadSingle()
Reads a 4-byte floating-point value from the current stream and advances the position in the stream by four bytes.
-
ReadString()
public virtual string ReadString()
Reads a string from the current stream. Reads a length-prefixed string, which means that the text is first read from the current stream.
-
ReadUInt16()
public virtual ushort ReadUInt16()
Reads a 2-byte unsigned integer from the current stream and advances the position in the stream by two bytes.
-
ReadUInt32()
public virtual uint ReadUInt32()
Reads a 4-byte unsigned integer from the current stream and advances the position in the stream by four bytes.
-
ReadUInt64()
public virtual ulong ReadUInt64()
Reads an 8-byte unsigned integer from the current stream and advances the position in the stream by eight bytes.
Example
using System;
using System.IO;
using System.Text;
public class Example
{
public static void Main()
{
string filePath = "data.bin";
// Write some binary data
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
using (BinaryWriter writer = new BinaryWriter(fileStream, Encoding.UTF8))
{
writer.Write(123); // int
writer.Write(3.14159); // double
writer.Write("Hello, Binary!"); // string
writer.Write(true); // bool
}
// Read the binary data
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
using (BinaryReader reader = new BinaryReader(fileStream, Encoding.UTF8))
{
int number = reader.ReadInt32();
double pi = reader.ReadDouble();
string message = reader.ReadString();
bool flag = reader.ReadBoolean();
Console.WriteLine($"Read int: {number}");
Console.WriteLine($"Read double: {pi}");
Console.WriteLine($"Read string: {message}");
Console.WriteLine($"Read bool: {flag}");
}
}
}