Advertisement



< Prev
Next >



C# BinaryWriter



So far, we have only discussed classes that are used to write and read bytes or characters. But what if we want to read and write data primitive values like char, int, float, double or even String? To achieve that, C# provides us the BinaryWriter class, which is used to create a stream, which can be used tox write the primitive types values or String values in binary to a stream.




Some commonly used constructors of BinaryWriter


Constructors Description
BinaryWriter(System.IO.Stream stream)
This constructor creates an object of the BinaryWriter based on the specified stream and using the default UTF-8 encoding.

FileStream(System.IO.Stream stream, System.Text.Encoding encoding)
The constructor created an object of the BinaryWriter class based on the specified Stream and character encoding.






Some commonly used write methods of BinaryWriter


Methods Description
void Write(int value)
This method writes a signed int value to the stream.

void Write(uint s)
This method writes an unsigned int value to the stream.

void Write(char ch)
This method writes a character value to the stream.

void Write(char[] array)
This method writes a character array to the stream.

void Write(short s)
This method writes a signed short value to the stream.

void Write(ushort s)
This method writes an unsigned short value to the stream.

void Write(byte b)
This method writes an unsigned byte value to the stream.

void Write(sbyte sb)
This method writes an signed byte value to the stream.

void Write(byte[] array)
This method writes a byte array to the stream.

void Write(float f)
This method writes a float to the stream.

void Write(double d)
This method writes a double to the stream.

void Write(long l)
This method writes a long to the stream.

void Write(String str)
This method writes a String to the stream.

void Flush()
This method closes this stream and also frees any resources connected with this stream.

void Close()
This method closes this stream and also frees any resources connected with this stream.






In this example, we are going create an object of the BinaryWriter class and use it to write different types of primitive values and a String object to a file, by using the default UTF-8 encoding.

//C# Example of BinaryWriter by using the default UTF-8 encoding.


using System;
using System.IO;

class A
{
public static void Main(String[] ar)
{
	try
	{
		//Creating variables with primitives values
		int i = -10;
		uint ui = 9;
		char ch = 'C';
		short s = -99;
		ushort us = 99;
		float f = 89.9f;
		double d = 875.888;
		long l = 10000000000;
			
		//Creating a String object
		String str = "Keep smiling and think positive.";
	

		//Creating an object of FileStream to write to a file
		//We are going to use the FileMode Create to create a new file to write to it
		FileStream fs = new FileStream("D:\\File3.txt", FileMode.Create);


		//Creating an object of BinaryWriter to write to a file using FileStream 
		BinaryWriter bw = new BinaryWriter(fs);

		Console.WriteLine("Writing to the file using BinaryStream and default UTF-8 Encoding");

		//writing an int 
		bw.Write(i);

		//writing an uint 
		bw.Write(ui);

		//writing a char
		bw.Write(ch);

		//writing a short
		bw.Write(s);

		//writing a ushort
		bw.Write(us);

		//writing a float 
		bw.Write(f);

		//writing a double
		bw.Write(d);

		//writing a long
		bw.Write(l);

		//writing a String 
		bw.Write(str);
		
		
		Console.WriteLine("Data written successfully");
	
	
		//Calling the Flush() method to flush the buffered data to the file
		bw.Flush();
		
		//Closing the stream i.e. FileStream stream
		fs.Close();

		//Closing the stream i.e. BinaryWriter stream
		bw.Close();
	}
	catch(IOException e)
	{
		Console.WriteLine(e);
	}
}
}


Output is -:


Writing to the file using BinaryStream and default UTF-8 Encoding
Data written successfully
In this example, we are going create an object of the BinaryWriter class and use it to write different types of primitive values and a String object to a file, by using the non-default Unicode encoding.

//C# Example of BinaryWriter by using the non-default Unicode encoding.


//C# Example of BinaryWriter


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

class A
{
public static void Main(String[] ar)
{
	try
	{
		//Creating variables with primitives values
		int i = -10;
		uint ui = 9;
		char ch = 'C';
		short s = -99;
		ushort us = 99;
		float f = 89.9f;
		double d = 875.888;
		long l = 10000000000;
			
		//Creating a String object
		String str = "Keep smiling and think positive.";
	
	
		//Creating an object of Encoding type
		Encoding unicode = Encoding.Unicode;


		//Creating an object of FileStream to write to a file
		//We are going to use the FileMode Create to create a new file to write to it
		FileStream fs = new FileStream("D:\\File5.txt", FileMode.Create);


		//Creating an object of BinaryWriter to write to a file using FileStream 
		BinaryWriter bw = new BinaryWriter(fs, unicode);

		Console.WriteLine("Writing to the file using BinaryStream and non-default Unicode Encoding");

		//writing an int 
		bw.Write(i);

		//writing an uint 
		bw.Write(ui);

		//writing a char
		bw.Write(ch);

		//writing a short
		bw.Write(s);

		//writing a ushort
		bw.Write(us);

		//writing a float 
		bw.Write(f);

		//writing a double
		bw.Write(d);

		//writing a long
		bw.Write(l);

		//writing a String 
		bw.Write(str);
	
		
		Console.WriteLine("Data written successfully");
		
		
		//Calling the Flush() method to flush the buffered data to the file
		bw.Flush();
		
		//Closing the stream i.e. FileStream stream
		fs.Close();

		//Closing the stream i.e. BinaryWriter stream
		bw.Close();
	}
	catch(IOException e)
	{
		Console.WriteLine(e);
	}
}
}


Output is -:


Writing to the file using BinaryStream and non-default Unicode Encoding
Data written successfully

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement