Advertisement
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. |
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 clears the buffer for the current writer stream by writing any buffered data to the stream. |
void Close() | This method closes this stream and also frees any resources connected with this stream. |
//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);
}
}
}
Writing to the file using BinaryStream and default UTF-8 Encoding
Data written successfully
öÿÿÿ Cÿc Í̳BÉv¾Ÿ_‹@ äT Keep smiling and think positive.
Advertisement
//C# Example of BinaryWriter by using the non-default Unicode encoding.
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);
}
}
}
Writing to the file using BinaryStream and non-default Unicode Encoding
Data written successfully
öÿÿÿ C ÿc Í̳BÉv¾Ÿ_‹@ äT @K e e p s m i l i n g a n d t h i n k p o s i t i v e .
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement