Reading and Writing Binary Files

You can read and write data to binary files using the BinaryReader and BinaryWriter classes. Both require a FileStream instance and enable you to read and write arrays of bytes. The following is an example of creating a binary stream:

Dim fs As New FileStream("C:\Temporary\OneFile.bin", FileMode.CreateNew)Dim bs As New BinaryWriter(fs)Dim bytesToWrite() As Byte = New Byte() {128, 64, 32, 16}bs.Write(bytesToWrite)bs.Close()fs.Close()

The Write method enables you to write information as binary, but it also accepts base .NET types such as integers and strings, all written as binary. It provides several overloads so that you can also specify the offset and the number of bytes to be written. ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.