C# Reading and writing binary files
BinaryReaderandBinaryWriterThe class is used for reading and writing binary files.
BinaryReader class
BinaryReaderA class is used to read binary data from files.BinaryReaderThe object is created by passing to its constructorFileStreamCreated as an object.
The table below listsBinaryReaderSome commonly used ones in the class.Methods:
| Serial Number | Method & Description |
|---|---|
| 1 | public override void Close() Close the BinaryReader object and the underlying stream. |
| 2 | public virtual int Read() Reads characters from the underlying stream and advances the current position of the stream. |
| 3 | public virtual bool ReadBoolean() Reads a Boolean value from the current stream and advances the current position of the stream by one byte. |
| 4 | public virtual byte ReadByte() Reads the next byte from the current stream and advances the current position of the stream by one byte. |
| 5 | public virtual byte[] ReadBytes(
int count
)
Reads the specified number of bytes from the current stream into a byte array and advances the current position of the stream by the specified number of bytes. |
| 6 | public virtual char ReadChar() Reads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character read from the stream. |
| 7 | public virtual char[] ReadChars(
int count
) Reads a specified number of characters from the current stream, returns the data in a character array, and advances the current position of the stream in accordance with the Encoding used and the specific characters read from the stream. |
| 8 | public virtual double ReadDouble() Reads an 8-byte floating-point value from the current stream and advances the current position of the stream by eight bytes. |
| 9 | public virtual int ReadInt32() Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes. |
| 10 | public virtual string ReadString()
Reads a string from the current stream. The string is prefixed with its length, encoded as a seven-bit integer. |
For the complete list of methods, see the Microsoft C# documentation.
BinaryWriter class
BinaryWriterA class is used to write binary data to files.BinaryWriterThe object is created by passing to its constructorFileStreamCreated as an object.
The table below listsBinaryWriterSome commonly used ones in the class.Methods:
| Serial Number | Method & Description |
|---|---|
| 1 | public override void Close() Close the BinaryWriter object and the underlying stream. |
| 2 | public virtual void Flush() Clears all buffers for the current writer and causes all buffered data to be written to the underlying device. |
| 3 | public virtual long Seek(
int offset,
SeekOrigin origin
)
Sets the position within the current stream. |
| 4 | public virtual void Write(
bool value
)
Writes a one-byte Boolean value to the current stream, where 0 represents false and 1 represents true. |
| 5 | public virtual void Write(
byte value
)
Writes an unsigned byte to the current stream and advances the stream position by one byte. |
| 6 | public virtual void Write(
byte[] buffer
)
Writes a byte array to the underlying stream. |
| 7 | public virtual void Write(
char ch
)
Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specified character to be written to the stream. |
| 8 | public virtual void Write(
char[] chars
)
Writes a character array to the current stream and advances the current position of the stream in accordance with the Encoding used and the specified characters to be written to the stream. |
| 9 | public virtual void Write(
double value
)
Writes an 8-byte floating-point value to the current stream and advances the stream position by eight bytes. |
| 10 | public virtual void Write(
int value
)
Writes a 4-byte signed integer to the current stream and advances the stream position by four bytes. |
| 11 | public virtual void Write(
string value
)
Writes a length-prefixed string to the stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the Encoding used and the specified characters to be written to the stream. |
For the complete list of methods, see the Microsoft C# documentation.
Example
The following example demonstrates reading and writing binary data:
Example
using System.IO;
namespace BinaryFileApplication
{
class Program
{
static void Main(string[] args)
{
BinaryWriter bw;
BinaryReader br;
int i = 25;
double d = 3.14157;
bool b = true;
string s = "I am happy";
// create file
try
{
bw = new BinaryWriter(new FileStream("mydata",
FileMode.Create));
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot create file.");
return;
}
// Write file
try
{
bw.Write(i);
bw.Write(d);
bw.Write(b);
bw.Write(s);
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot write to file.");
return;
}
bw.Close();
// Read file
try
{
br = new BinaryReader(new FileStream("mydata",
FileMode.Open));
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot open file.");
return;
}
try
{
i = br.ReadInt32();
Console.WriteLine("Integer data: {0}", i);
d = br.ReadDouble();
Console.WriteLine("Double data: {0}", d);
b = br.ReadBoolean();
Console.WriteLine("Boolean data: {0}", b);
s = br.ReadString();
Console.WriteLine("String data: {0}", s);
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot read from file.");
return;
}
br.Close();
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following results:
Integer data: 25 Double data: 3.14157 Boolean data: True String data: I am happyother extensions