Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Reading Binary Data from a File

To read data from a binary file, you must first create a BinaryReader that's connected to the FileStream . Assuming that fs refers to the FileStream object associated with the file, this is the syntax:

Dim br As BinaryReader
br = New BinaryReader(fs)

The BinaryReader class has a variety of methods for reading data from the file. These methods fall into two categories:

  • Datatype-specific methods for reading a single unit of data (a byte, an Int16, and so on) into a program variable of the corresponding type
  • Nonspecific methods that read an arbitrary number of bytes into an array

These methods are described in Table 5.

Table 5 Methods of the BinaryReader Class.

Method

Action

PeekChar()

Returns the next byte from the file but doesn't advance the file pointer. Returns -1 if the pointer is at the end of the file.

ReadBoolean()

Reads a type Boolean from the file.

ReadByte()

Reads a type Byte from the file.

ReadBytes(n)

Reads n bytes from the file and returns them in a byte array.

ReadChar()

Reads a type Char from the file.

ReadChars(n)

Reads n characters from the file and returns them in a character array.

ReadDecimal()

Reads a type Decimal from the file.

ReadDouble()

Reads a type Double from the file.

ReadInt16()

Reads a type Short from the file.

ReadInt32()

Reads a type Integer from the file.

ReadInt64()

Reads a type Long from the file.

ReadSingle()

Reads a type Single from the file.

It's important for you to be aware that these methods have no way of knowing what's stored in the file. All they see is a sequence of bytes. Do the next 8 bytes in the file represent one Int64, two Int32s, two Doubles, or four Int16s? There's no way to tell except by knowing how the file was written.

With the exception of PeekChar(), all of these methods advance the file pointer as needed. This lets you call these methods repeatedly to read the entire contents of a file in sequence. Whichever method you're using, your code needs some way to avoid trying to read beyond the end of the file, which would cause an EndOfStreamException. There are several techniques you can use to deal with the end of file:

  • You may know the type and number of data items in the file; for example, if your program created the file and now needs to read from it. If you stored, say, 200 type Double data items in the file, you know that you can read 200 type Double data items back from the file without an end-of-file exception.
  • Obtain the file length from the FileStream object's Length property and divide by the length of the data item that the file holds. For example, suppose that the file length is 1000 and the data it holds is type Short. Because a type Short is 2 bytes long, you know that the file contains 1000/2 = 500 data items. To get information on the length of Visual Basic's datatypes, see the datatype summary in the Help information.

When finished reading data, be sure to call the BinaryReader.Close() method to close the reader and the underlying FileStream.

  • + Share This
  • 🔖 Save To Your Account