The BinaryReader and BinaryWriter classes are not stream classes. Instead, they are helper classes that work with stream classes. They let you read and write data in files using a specific encoding. For example, the BinaryReader object’s ReadInt32 method reads a 4-byte (32-bit) signed integer from the stream. Similarly, the ReadUInt16 method reads a 2-byte (16-bit) unsigned integer.
These classes still work at a relatively low level, and you should generally use higher-level classes to read and write data if possible. For example, you shouldn’t tie yourself to a particular representation of an integer (32- or 16-bit) unless you really must.
BinaryReader and BinaryWriter objects are attached to stream objects that provide access to the underlying bytes. Both of these classes have a BaseStream property that returns a reference to the underlying stream. Note also that the Close method provided by each of these classes automatically closes the underlying stream.
The following table describes the BinaryReader class’s most useful methods.
METHOD | PURPOSE |
Close | Closes the BinaryReader and its underlying stream. |
PeekChar | Reads the stream’s next character but does not advance the reader’s position, so other methods can still read the character later. |
Read | Reads characters from the stream and advances the reader’s position. |
ReadBoolean | Reads a Boolean from the stream and advances the reader’s position by 1 byte. |
ReadByte | Reads a byte from the stream and advances the reader’s position by 1 byte. |
ReadBytes | Reads a number of bytes from the stream into a byte array and advances the reader’s position by that number of bytes. |
ReadChar | Reads a character from the stream and advances the reader’s position appropriately for the stream’s encoding. |
ReadChars | Reads a number of characters from the stream, returns the results in a character array, and advances the reader’s position appropriately for the stream’s encoding. |
ReadDecimal | Reads a decimal value from the stream and advances the reader’s position by 16 bytes. |
ReadDouble | Reads an 8-byte floating-point value from the stream and advances the reader’s position by 8 bytes. |
ReadInt16 | Reads a 2-byte signed integer from the stream and advances the reader’s position by 2 bytes. |
ReadInt32 | Reads a 4-byte signed integer from the stream and advances the reader’s position by 4 bytes. |
ReadInt64 | Reads an 8-byte signed integer from the stream and advances the reader’s position by 8 bytes. |
ReadSByte | Reads a signed byte from the stream and advances the reader’s position by 1 byte. |
ReadSingle | Reads a 4-byte floating-point value from the stream and advances the reader’s position by 4 bytes. |
ReadString | Reads a string from the current stream and advances the reader’s position past it. The string begins with its length. |
ReadUInt16 | Reads a 2-byte unsigned integer from the stream and advances the reader’s position by 2 bytes. |
ReadUInt32 | Reads a 4-byte unsigned integer from the stream and advances the reader’s position by 4 bytes. |
ReadUInt64 | Reads an 8-byte unsigned integer from the stream and advances the reader’s position by 8 bytes. |
The following table describes the BinaryWriter class’s most useful methods.
METHOD | PURPOSE |
Close | Closes the BinaryWriter and its underlying stream. |
Flush | Writes any buffered data into the underlying stream. |
Seek | Sets the position within the stream. |
Write | Writes a value into the stream. This method has many overloaded versions that write characters, arrays of characters, integers, strings, unsigned 64-bit integers, and so forth. |
You can learn about the other members of the BinaryWriter and BinaryReader classes at http://msdn.microsoft.com/system.io.binarywriter.aspx and http://msdn.microsoft.com/system.io.binaryreader.aspx, respectively.