In the C# programming language, a BinaryReader is a class that is used to read binary data from a stream. It is found in the System.IO namespace. It also supports reading a string in a specific encoding. The BinaryReader class belongs to the System.IO namespace, and it is used to read primitive data types. The BinaryReader uses the UTF-8 encoding, which helps to read the data until we define another character encoding during the creation of its object.
Syntax:
It has the following syntax.
BinaryReader reader = new BinaryReader(Stream input);
Or with encoding:
BinaryReader reader = new BinaryReader(Stream input, Encoding encoding);
In this syntax,
- Stream input: It represents the stream, such as a FileStream from which the binary data is to be read.
- Encoding encoding: It specifies the character encoding according to need.
Simple C# BinaryReader Class Example
Let us take an example to illustrate the BinaryReader class in C#.
Example
using System;
using System.IO;
class C# Tutorial
{
static void Main()
{
string filePath = "example.bin";
using(BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
{
writer.Write(555); // Write an integer
writer.Write(45.67); // Write a double
writer.Write("Hello, Welcome to C# Programming tech"); // Write a string
}
// Reading the data of BinaryReader
using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
{
int intValue = reader.ReadInt32();
double doubleValue = reader.ReadDouble();
string stringValue = reader.ReadString();
Console.WriteLine("The integer value is " + intValue);
Console.WriteLine("The double value is " + doubleValue);
Console.WriteLine("The string value is " + stringValue);
}
}
}
Output:
The integer value is 555
The double value is 45.67
The string value is Hello, Welcome to C# Programming tech
Explanation:
In this example, we demonstrate the use of the BinaryReader classes in C#. First, we create a binary file named example.bin, which writes three types of data: integer, double, and string. After that, we use the BinaryReader class to read the data from the file. At last, we are using the Console.WriteLine method to print the read data.
Methods of the BinaryReader Class in C#
In C# , the BinaryReader Class has several functions, which are as follows.
| Method | Description |
|---|---|
| Read() | The Read() method is commonly utilized to read the characters from the current stream. |
| ReadByte() | The ReadByte() method is utilized to read the next byte from the latest position in the binary stream. |
| ReadBoolean() | The ReadBoolean() method is commonly utilized to read the Boolean value from the current position in the binary stream. |
| ReadDouble() | The ReadDouble() method is commonly utilized to read the floating point value from the current position in the binary stream. |
| ReadString() | The ReadStriing() method is utilized to read the string value from the current stream. |
| ReadChar() | The ReadChar() method is commonly utilized to extract data from a file. |
| ReadChars(int count) | It is used to read the specified number of characters from a stream or source. |
| ReadInt32() | It is commonly utilized to read the 4-byte signed integer from the existing stream and enhance the current position of the stream by four bytes. |
| ReadInt16() | It is commonly utilized to read the 2-byte signed integer from the current position of the stream. |
| ReadDecimal() | It is utilized to read the decimal value from the current position of the stream. |
| Close() | The Close() method is utilized to close the BinaryReader and the underlying stream. |
C# Example to Read and Write Multiple Data Types Using BinaryReader and BinaryWriter
Let us take an example to define the methods of the BinaryReader class in C#.
Example
using System;
using System.IO;
class C# Tutorial
{
static void Main()
{
string filePath = "ex.bin";
using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
{
writer.Write(125);
writer.Write(35.75);
writer.Write(true);
writer.Write('M');
writer.Write("Hello, Welcome to C# Programming");
}
using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
{
int n = reader.ReadInt32();
double p = reader.ReadDouble();
bool s = reader.ReadBoolean();
char g = reader.ReadChar();
string m = reader.ReadString();
Console.WriteLine("The integer value is " + n);
Console.WriteLine("The double value is " + p);
Console.WriteLine("The boolean value is " + s);
Console.WriteLine("TThe character value is " + g);
Console.WriteLine("The string value is " + m);
}
}
}
Output:
The integer value is 125
The double value is 35.75
The boolean value is True
The character value is M
The string value is Hello, Welcome to C# Programming
Explanation:
In this example, we create a file named ex.bin using BinaryWriter . The BinaryWriter class converts each value into its binary representation. The BinaryReader class reads the same file and retrieves the data in the same order. After that, we use the Console.WriteLine method to print the retrieved values.
C# BinaryReader Function Example with ReadString Method
Let us take an example to illustrate the ReadString method in the BinaryReader class in C#.
Example
using System;
using System.IO;
class C# Tutorial
{
static void Main()
{
string path = "exp.bin";
// Writing data to a binary file
using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create)))
{
writer.Write("Welcome to C# Programming");
}
// Reading data from the binary file
using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open)))
{
string mes = reader.ReadString();
Console.WriteLine("Message: " + mes);
}
}
}
Output:
Message: Welcome to C# Programming
Explanation:
In the example, we have taken a class named C# Tutorial. In the main method, we have created a binary file named exp.bin using the Binary class. After that, we use the BinaryReader class to read the same string from the file, and use the Console.WriteLine function to display the result.
Features of the BinaryReader Method in C#
Several features of the BinaryReader Method in C# sare as follows:
- It is commonly used to read primitive data types as binary values rather than text.
- It is very helpful to read the data in the same order in which it was written in BinaryWriter in C#.
- It uses IDisposable in C# programs, which enables the resources to be released automatically.
- It provides read characters with custom text encodings.
- It helps in error handling during the binary tasks in C#.
Conclusion
In conclusion, a BinaryReader is a class that is used to read binary information from a stream. It also supports readinsg a string in a specific encoding. It provides several methods to read primitive data types, including integers, doubles, booleans, strings , and many others. It helps to read the code easily and safely from different sources.
C# BinaryReader FAQs
1) What is the BinaryReader class in C#?
In C#, the BinaryReader is a class that is used to read binary information from a stream. It is found in the System.IO namespace. It also supports reading a string in a specific encoding. The BinaryReader class belongs to the System.IO namespace.
2) Which namespace contains the BinaryReader class in C#?
The BinaryReader class is in the System.IO namespace. This namespace has classes for performing input and output operations, such as reading and writing data to files and streams.
3) How do we create a BinaryReader object?
In the C# programming language, we can create a BinaryReader object. We need to pass a stream to its constructor. It has the following syntax.
BinaryReader reader = new BinaryReader(Stream input);
4) What is the ReadInt32 method in the BinaryReader class in C#?
In C#, the ReadInt32 method is used to read the 4-byte signed integer from the existing stream and advances the current position of the stream by four bytes.
5) What is the ReadString method in the BinaryReader class in C#?
In the C# programming language, the ReadString method is used to read the string value from the current stream. This method first reads the length of the string that was written to the stream, and then reads that many characters to reconstruct the original string.