MorkaLork Development

Interesting stuff I've picked up over the years...

Input/Output 13: BinaryReader

2009-04-17 07:18:48 | 297 views | csharp class method tutorial binary reader binaryreader

The Basics

You use the BinaryReader class to read binary data from a file. You can construct a BinaryReader object by passing its constructor a Stream object; you can't pass a file path to create a BinaryReader object. Once you have a BinaryReader object, you can use one of its ReadXXX methods to read various types of data. For example, the ReadByte method is used to read a byte, and the ReadString method is used to read a String. The data must be read in the order it was written. For example, if you store a byte and then a string to a binary file, you must first read a byte and then read a String. The BinaryReader class also has a Read method, which has three overloads that allow you to read a character, an array of bytes, or an array of characters. As an example, here is the code that reads a byte and a string from a binary file called MyBinary.bin:

BinaryReader br = new BinaryReader(File.OpenRead("
Byte b2 = br.ReadByte();
string s2 = br.ReadString();


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.