Introduction to BinaryWriter in C#
In C#, BinaryWriter is a class used to write primitive types as binary data in a particular encoding stream. It is present under System.IO namespace.
Following are some important points regarding BinaryWriter:
- BinaryWriter is used to create binary files.
- BinaryWriter can be used to write strings in a specific encoding.
- In order to create an object of BinaryWriter, we need to pass an object of Stream to the constructor of the BinaryWriter class.
- While creating the object of BinaryWriter, if we do not specify any encoding then by default UTF-8 encoding will be used.
Syntax with Explanation
The constructor to create an object of BinaryWriter is available in four overloaded forms. The syntax to create a BinaryWriter object using all its overloaded constructors are as follows:
It is used to initialize an instance of the BinaryWriter class.
BinaryWriter binaryWriter = new BinaryWriter(outputStream) ;
The above statement initializes a new instance of BinaryWriter class on the basis of the specified stream (outputStream) and using UTF-8 character encoding.
BinaryWriter binaryWriter = new BinaryWriter(outputStream, encoding);
The above statement initializes a new instance of BinaryWriter based on the specified stream (outputStream) and character encoding (encoding).
BinaryWriter binaryWriter = new BinaryWriter(outputStream, encoding, true);
- The above statement works similar to the second and the third statements, except it has an extra parameter of data type Boolean which one can use to indicate whether he wants to keep the output stream open after the BinaryWriter object is disposed of.
- To leave the stream open the value of the Boolean parameter should be set to ‘true’ otherwise it should be set to ‘false’.
- We can create the object of BinaryWriter class inside ‘using’ block so that the memory occupied by the object will be released automatically when the work of the object is completed and it is no longer needed.
using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(fileName, FileMode.Create )) )
Here, File.Open() method returns FileStream object which helps to create an instance of BinaryWriter.
How BinaryWriter Works in C#?
- In C#, BinaryWriter is used to write binary data to a file or we can say that it is used to create binary files. It helps us write primitive data types in binary format to a stream. It also helps us write strings in a particular character encoding.
- To work with BinaryWriter, it is necessary to import System.IO namespace in the program. Then, we can create the object of the BinaryWriter class by using ‘new’ operator and bypassing a Stream object to the constructor of BinaryWriter.
- To create an instance of BinaryWriter, we generally provide Stream object to its constructor at the same time we can provide an optional parameter that specifies the encoding to be used while writing the file. In case, the user does not provide any character encoding then UTF-8 encoding will be used by default.
- There is another optional parameter that can be passed to the constructor while creating the object of BinaryWriter. This parameter is of type Boolean and is used to specify whether the user wants the current stream to remain open or not after the BinaryWriter object is disposed of.
- BinaryWriter class provides different Write() methods for different types of data. These methods are used to write data to the binary file. As Write(Int32) method is used to write four-byte signed integer to the current stream and also it advances the stream position by four bytes.
Methods of BinaryWriter
Following table shows details of some Write() methods of BinaryWriter for different data types:
|Write(Boolean)||This method is used to write the one-byte Boolean value to the present stream; 0 represents false while 1 represents true.|
|Write(Byte)||This method is used to write an unsigned byte to the present stream and then it advances the position of the stream by one byte.|
|Write(Char)||This method is used to write Unicode character to present stream and also it advances the present stream position according to the character encoding used and according to the characters being written to the present stream.|
|Write(Decimal)||This method is used to write a decimal value to the present stream and also it advances the position of the current stream by sixteen bytes.|
|Write(Double)||This method is used to write an eight-byte floating-point value to the present stream and then it also advances the position of the current stream by eight bytes.|
|Write(Int32)||This method is used to write a four-byte signed integer to the present stream and then it advances the position of current stream by four bytes.|
|Write(String)||This method is used to write length prefixed string to present stream in the present encoding of BinaryWriter and also it advances the current stream position according to the encoding used and according to the characters being written to the present stream.|
Examples to Implement BinaryWriter in C#
Example showing the creation of file:
public class Demo
string fileLocation = "E:\\Content\\newBinaryFile.dat";
public void WritingFile()
//checking if file exists
FileStream fileStream = new FileStream(fileLocation, FileMode.Create,
//creating binary file using BinaryWriter
using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
//writing data using different Write() methods
binaryWriter.Write("This is a string value.");
catch (Exception exception)
public void ReadingFile()
FileStream fileStream = new FileStream(fileLocation, FileMode.Open,
using (BinaryReader binaryReader = new BinaryReader(fileStream))
Console.WriteLine("IntegerValue = " + binaryReader.ReadInt32());
Console.WriteLine("StringValue = " + binaryReader.ReadString());
Console.WriteLine("CharValue = " + binaryReader.ReadChar());
catch (Exception exception)
public class BinaryWriterDemo
static void Main(string args)
Demo demoObj = new Demo();
In C#, the BinaryWriter class is used to write primitive types as binary information to the stream. If the encoding is not defined, then the BinaryWriter class uses the default UTF-8 character encoding to write data to a binary file. An object of BinaryWriter can be created using the Stream object.
This is a guide to BinaryWriter in C#. Here we discuss syntax and explanation, how does it works with examples to implement with proper codes and outputs. You can also go through our other related articles to learn more –