Saturday 14 September 2013

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.NMethod Name & Purpose
1Public Overridable Sub Close
It closes the BinaryReader object and the underlying stream.
2Public Overridable Function Read As Integer
Reads the characters from the underlying stream and advances the current position of the stream.
3Public Overridable Function ReadBoolean As Boolean
Reads a Boolean value from the current stream and advances the current position of the stream by one byte.
4Public Overridable Function ReadByte As Byte
Reads the next byte from the current stream and advances the current position of the stream by one byte.
5Public 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.
6Public Overridable Function ReadChar As Char
Reads 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.
7Public 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.
8Public Overridable Function ReadDouble As Double
Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
9Public Overridable Function ReadInt32 As Integer
Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
10Public 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.NFunction Name & Description
1Public Overridable Sub Close
It closes the BinaryWriter object and the underlying stream.
2Public Overridable Sub Flush
Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
3Public Overridable Function Seek ( offset As Integer, origin As SeekOrigin ) As Long 
Sets the position within the current stream.
4Public Overridable Sub Write ( value As Boolean ) 
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.
5Public Overridable Sub Write ( value As Byte ) 
Writes an unsigned byte to the current stream and advances the stream position by one byte.
6Public Overridable Sub Write ( buffer As Byte() ) 
Writes a byte array to the underlying stream.
7Public 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.
8Public 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.
9Public 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.
10Public Overridable Sub Write ( value As Integer ) 
Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.
11Public 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.
For complete list of methods, please visit Microsoft's documentation.

Example

The following example demonstrates reading and writing binary data:
Imports System.IO
Module fileProg
   Sub Main()
      Dim bw As BinaryWriter
      Dim br As BinaryReader
      Dim i As Integer = 25
      Dim d As Double = 3.14157
      Dim b As Boolean = True
      Dim s As String = "I am happy"
      'create the file
      Try
          bw = New BinaryWriter(New FileStream("mydata", FileMode.Create))
      Catch e As IOException
          Console.WriteLine(e.Message + "\n Cannot create file.")
          Return
      End Try
      'writing into the file
      Try
          bw.Write(i)
          bw.Write(d)
          bw.Write(b)
          bw.Write(s)
      Catch e As IOException
          Console.WriteLine(e.Message + "\n Cannot write to file.")
          Return
      End Try
      bw.Close()
      'reading from the file
      Try
          br = New BinaryReader(New FileStream("mydata", FileMode.Open))
      Catch e As IOException
          Console.WriteLine(e.Message + "\n Cannot open file.")
          Return
      End Try
      Try
           i = br.ReadInt32()
          Console.WriteLine("Integer data: {0}", i)
          d = br.ReadDouble()
          Console.WriteLine("Double data: {0}", d)
          b = br.ReadBoolean()
          Console.WriteLine("Boolean data: {0}", b)
          s = br.ReadString()
          Console.WriteLine("String data: {0}", s)
      Catch e As IOException
          Console.WriteLine(e.Message + "\n Cannot read from file.")
          Return
      End Try
      br.Close()
      Console.ReadKey()
   End Sub
End Module
When the above code is compiled and executed, it produces following result:
Integer data: 25
Double data: 3.14157
Boolean data: True
String data: I am happy

No comments:

Post a Comment