Advertisement
Constructors | Description |
---|---|
BinaryReader(System.IO.Stream stream) | This constructor creates an object of the BinaryReader 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 BinaryReader class based on the specified Stream and character encoding. |
Methods | Description |
---|---|
int ReadInt32() | This method reads a signed int value from the stream. |
uint ReadUInt32() | This method reads an unsigned int value from the stream. |
char ReadChar() | This method reads a character value from the stream. |
short ReadInt16() | This method reads a signed short value from the stream. |
ushort ReadUInt16() | This method reads an unsigned short value from the stream. |
byte ReadByte() | This method reads an unsigned byte value from the stream. |
sbyte ReadSByte() | This method reads an signed byte value from the stream. |
float ReadSingle() | This method reads a float from the stream. |
double ReadDouble() | This method reads a double from the stream. |
long ReadInt64() | This method reads a long from the stream. |
String ReadString() | This method reads a String from the stream. |
void Close() | This method closes this stream and also frees any resources connected with this stream. |
//C# Example of BinaryReader and BinaryWriter with default UTF-8 encoding.
using System;
using System.IO;
using System.Text;
class A
{
public static void Main(String[] ar)
{
A ob = new A();
ob.WriteValues();
ob.ReadValues();
}
public void WriteValues()
{
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 OpenOrCreate to create a new file to write to it
FileStream fs = new FileStream("D:\\File2.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 in memory");
//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);
//Calling the Flush() method to flush the buffered bytes to the internal array
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);
}
}
public void ReadValues()
{
try
{
//Creating an object of FileStream to read to a file
//We are going to use the FileMode Open to create a new file to write to it
FileStream fs = new FileStream("D:\\File2.txt", FileMode.Open);
//Creating an object of BinaryReader to read from a file using FileStream
BinaryReader br = new BinaryReader(fs);
Console.WriteLine("Reading values from a file using BinaryStream in memory");
//Reading an int
Console.WriteLine(br.ReadInt32());
//Reading an uint
Console.WriteLine(br.ReadUInt32());
//Reading a char
Console.WriteLine(br.ReadChar());
//Reading a short
Console.WriteLine(br.ReadInt16());
//Reading a ushort
Console.WriteLine(br.ReadUInt16());
//Reading a float
Console.WriteLine(br.ReadSingle());
//Reading a double
Console.WriteLine(br.ReadDouble());
//Reading a long
Console.WriteLine(br.ReadInt64());
//Reading a String
Console.WriteLine(br.ReadString());
//Closing the stream i.e. FileStream stream
fs.Close();
//Closing the stream i.e. BinaryReader stream
br.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Writing to the file using BinaryStream in memory
Reading values from a file using BinaryStream in memory
-10
9
C
-99
99
89.9
875.888
10000000000
Keep smiling and think positive.
öÿÿÿ Cÿc Í̳BÉv¾Ÿ_‹@ äT Keep smiling and think positive.
Advertisement
//C# Example of BinaryReader and BinaryWriter with default UTF-8 encoding.
using System;
using System.IO;
using System.Text;
class A
{
public static void Main(String[] ar)
{
A ob = new A();
ob.WriteValues();
ob.ReadValues();
}
public void WriteValues()
{
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 OpenOrCreate to create a new file to write to it
FileStream fs = new FileStream("D:\\File6.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 in memory");
//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);
//Calling the Flush() method to flush the buffered bytes to the internal array
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);
}
}
public void ReadValues()
{
try
{
//Creating an object of Encoding type
Encoding unicode = Encoding.Unicode;
//Creating an object of FileStream to read to a file
//We are going to use the FileMode OpenOrCreate to create a new file to write to it
FileStream fs = new FileStream("D:\\File6.txt", FileMode.Open);
//Creating an object of BinaryReader to read from a file using FileStream
BinaryReader br = new BinaryReader(fs, unicode);
Console.WriteLine("Reading values from a file using BinaryStream in memory");
//Reading an int
Console.WriteLine(br.ReadInt32());
//Reading an uint
Console.WriteLine(br.ReadUInt32());
//Reading a char
Console.WriteLine(br.ReadChar());
//Reading a short
Console.WriteLine(br.ReadInt16());
//Reading a ushort
Console.WriteLine(br.ReadUInt16());
//Reading a float
Console.WriteLine(br.ReadSingle());
//Reading a double
Console.WriteLine(br.ReadDouble());
//Reading a long
Console.WriteLine(br.ReadInt64());
//Reading a String
Console.WriteLine(br.ReadString());
//Closing the stream i.e. FileStream stream
fs.Close();
//Closing the stream i.e. BinaryReader stream
br.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Writing to the file using BinaryStream in memory
Reading values from a file using BinaryStream in memory
-10
9
C
-99
99
89.9
875.888
10000000000
Keep smiling and think positive.
öÿÿÿ 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