Binary primitives can be written to and read from files directly using the BinaryWriter and BinaryReader classes. You can use these classes to create your own binary file format for your application. If you deal only with primitive types, this is the best stream to use. Remember that this data is not easily readable by a human eying its contents since the data is read in its binary form.
BinaryWriter class
This class is used to write primitive data types and strings. The BinaryWriter type exposes the following members.
Close Method - This method is used to close the current BinaryWriter and the underlying stream.
Write(Boolean) - This method is used to write a one-byte Boolean value to the current stream.
Write(Byte) - This method is used to write an unsigned byte to the current stream and advances the stream position by one byte.
Write(Char) - This method is used to write 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.
Write(Decimal) - This method is used to write a decimal value to the current stream and advances the stream position by sixteen bytes.
Write(Double) - This method is used to write an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.
Write(Int16) - Writes a two-byte signed integer to the current stream and advances the stream position by two bytes. Write(Int32) Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.
Write(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 example
Imports System.Text
Imports System.IO
Module Module1
Sub Main()
Dim Writer As New BinaryWriter(File.Open("C:/test.bin", FileMode.Create))
Writer.Write("R")
Writer.Write(456)
Writer.Write(456.789)
Writer.Write("please test string here")
End Sub
End Module
BinaryReader class
BinaryReader is using for read primitive types as binary values in a specific encoding stream.
Example
Imports System.Text
Imports System.IO
Module Module1
Sub Main()
Dim BinaryFile As New FileStream("c:/test1.dat", FileMode.Create, FileAccess.ReadWrite)
Dim Reader As New BinaryReader(BinaryFile)
Dim Writer As New BinaryWriter(BinaryFile)
Writer.Write("a"c)
Writer.Write(123)
Writer.Write(456.789)
Writer.Write("test string")
Dim ReadChar As Char
Dim ReadDouble As Double
Dim ReadInteger As Integer
Dim ReadString As String
BinaryFile.Seek(0, SeekOrigin.Begin)
ReadChar = Reader.ReadChar()
ReadInteger = Reader.ReadInt32()
ReadDouble = Reader.ReadDouble()
ReadString = Reader.ReadString()
Console.WriteLine("Character: {0}", ReadChar)
Console.WriteLine("Integer: {0}", ReadInteger)
Console.WriteLine("Double: {0}", ReadDouble)
Console.WriteLine("String: {0}", ReadString)
End Sub
End Module
Output of above example
Conclusion
Hope this article would have helped you in understanding BinaryReader and BinaryWriter Classes in Visual Basic. If there is any mistake in this article then please notify me. I expect your valuable comments and feedback about this article.