The BinaryReader class provides the methods you need to read data from a binary file. As you have seen, binary files might also hold text, and the BinaryReader class provides the ReadString method to read strings written to the file by the WriteString method.
To use the methods of the BinaryReader class in your code, you must first create an instance of the class. The BinaryReader object must be associated with a FileStream object, and the simplest form of its constructor is the following, where streamObj is the FileStream object:
Code language: PHP (php)
Dim BR As New BinaryReader(streamObj)
You can also specify the character-encoding scheme to be used with the BR object, using the following form of the constructor:
Code language: PHP (php)
Dim BR As New BinaryReader(streamObj, encoding)
If you omit the encoding argument, the default UTF-8Encoding will be used.
The BinaryReader class exposes the following methods for accessing the contents of a binary file.
This method is the same as the Close method of the StreamReader class. It closes the current reader and releases the underlying stream.
This method returns the next available character from the stream without repositioning the current pointer. The character read is returned as an integer, or −1 if there are no more characters to be read from the stream. The name of the method doesn’t quite comply with the BinaryReader class, and here’s why. Peeking at the next byte makes sense only if the next byte is a character. Reading the first byte of a Double value, for example, wouldn’t help you much. A character is usually stored in a single byte (ASCII text), but it can also be stored in 2 bytes (Unicode text). The PeekChar method knows how many bytes it must read from the text (they’re determined by the current encoding), and it always returns a character, regardless of its size in bytes. The PeekChar method’s return value is an integer, not a character.
The Read Methods
The BinaryReader class exposes methods for reading the same base data types you can write to a file through the BinaryWriter class. Whereas there’s only one Write method that writes any data type to the binary file, there are many methods to read the same data. Each method returns a value of the corresponding type (the ReadBoolean method returns a Boolean value, the ReadDecimal returns a Decimal type, and so on) and only a single value of this type. To read multiple values of the same type, you must call the same method repeatedly. The various methods for reading the base data types from the file are briefly described in Table 11.7.
Table 11.7: The Read Methods of the BinaryReader Class
|Read||Reads the next character from the stream and returns it as an integer value. An overloaded form of the method reads a number of characters into an array of characters.|
|ReadBoolean||Reads and returns a True/False value.|
|ReadByte||Reads and returns a single byte.|
|ReadBytes(byteArray, count)||Reads and returns count bytes from the file and stores them into the Byte array passed as the first argument.|
|ReadChar||Reads and returns a character. Depending on how text was stored in the file, the ReadChar method might read 1 or 2 bytes (in the case of Unicode text), but it always returns a character.|
|ReadChars(charArray, count)||Reads and returns count characters from the file and stores them in the character array specified as the first argument.|
|ReadDecimal||Reads and returns a Decimal value from the file.|
|ReadDouble||Reads and returns a Double value from the file.|
|ReadInt16||Reads and returns a short Integer (two-byte) value.|
|ReadInt32||Reads and returns an Integer (four-byte) value.|
|ReadInt64||Reads and returns a Long Integer (eight-byte) value.|
|ReadSByte||Reads and returns a signed byte.|
|ReadSingle||Reads a Single (four-byte) value from the file.|
|ReadString||Reads and returns a string from the file. The string must be stored in the file prefixed by its length. This is how the WriteString method stored strings to a text file, so there’s nothing you have to do from within your code. If the string isn’t prefixed by its length, the ReadString method will read a string with the wrong number of characters. The method will interpret the first byte as the string’s length.|
|ReadUInt16||Reads and returns an unsigned short Integer (two-byte) value.|
|ReadUInt32||Reads and returns an unsigned Integer (four-byte) value.|
|ReadUInt64||Reads and returns an unsigned long Integer (eight-byte) value.|
To use these methods, you’re supposed to know the structure of the data stored in the file. A file with a price list, for example, contains the same items for each product. The first two fields are the product’s ID and description, followed by the product’s price and other pieces of information, which are repeated for each product. If you know the types of values stored in the file, you can call the appropriate methods to read the correct values. If you misread even a single value, none of the following values will be read correctly.