VB.Net - Reading from and Writing into Binary Files

The BinaryReader and BinaryWriter classes are used for reading from and writing to a binary file.

The BinaryReader Class

The BinaryReader class is used to read binary data from a file. A BinaryReader object is created by passing a FileStream object to its constructor.

The following table shows some of the commonly used methods of the BinaryReader class.

S.N

Method Name & Purpose

1

Public Overridable Sub CloseIt closes the BinaryReader object and the underlying stream.

2

Public Overridable Function Read As IntegerReads the characters from the underlying stream and advances the current position of the stream.

3

Public Overridable Function ReadBoolean As BooleanReads a Boolean value from the current stream and advances the current position of the stream by one byte.

4

Public Overridable Function ReadByte As ByteReads the next byte from the current stream and advances the current position of the stream by one byte.

5

Public Overridable Function ReadBytes (
count As Integer
) As Byte()
Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.

6

Public Overridable Function ReadChar As CharReads 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 being read from the stream.

7

Public Overridable Function ReadChars (
count As Integer
) As Char()
Reads the specified number of characters from the current stream, returns the data in a character array, and advances the current position in accordance with the Encoding used and the specific character being read from the stream.

8

Public Overridable Function ReadDouble As DoubleReads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.

9

Public Overridable Function ReadInt32 As IntegerReads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.

10

Public Overridable Function ReadString As String
Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.

The BinaryWriter Class

The BinaryWriter class is used to write binary data to a stream. A BinaryWriter object is created by passing a FileStream object to its constructor.

The following table shows some of the commonly used methods of the BinaryWriter class.

S.N

Function Name & Description

1

Public Overridable Sub CloseIt closes the BinaryWriter object and the underlying stream.

2

Public Overridable Sub FlushClears all buffers for the current writer and causes any buffered data to be written to the underlying device.

3

Public Overridable Function Seek (
offset As Integer,
origin As SeekOrigin
) As Long
Sets the position within the current stream.

4

Public Overridable Sub Write (
value As Boolean
)
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.

5

Public Overridable Sub Write (
value As Byte
)
Writes an unsigned byte to the current stream and advances the stream position by one byte.

Public Overridable Sub Write (
ch As Char
)
Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

8

Public Overridable Sub Write (
chars As Char()
)
Writes a character array to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

9

Public Overridable Sub Write (
value As Double
)
Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.

10

Public Overridable Sub Write (
value As Integer
)
Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.

11

Public Overridable Sub Write (
value As String
)
Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.