|
|
How Do I...Read and write binary data?
This sample illustrates basic binary file input and output using the BinaryReader, BinaryWriter and FileStream classes. A similar topic is presented under the heading How Do I...Create a log file?. Reading and writing binary information enables you to create and use files that are not accessible through other input and output methods. This example also demonstrates writing non-string data, and demonstrates the capabilities of binary I/O.
Although files on your computer can be stored in different types and files, one of the more common formats for files is a binary format. This short introduction to creating binary files uses the base classes BinaryReader and BinaryWriter to get information from, and put information into a file. Each of these classes wraps a stream of information, so before going further, you need create a stream which you can use to write your information to and from. Because you will be creating files, you will use a FileStream to expose a specific file, which in this case, you can modify if it already exists, or create if it does not exist. Once you have your FileStream, you can use it to construct your BinaryReader and BinaryWriter, as in the following example.
' Make a new FileStream object, exposing our data file.
' If the file exists, open it, and if it doesn't, then Create it.
Dim fs As FileStream = New FileStream("data.bin", FileMode.OpenOrCreate)
' create the reader and writer, based on our file stream
Dim w As BinaryWriter = New BinaryWriter(fs)
Dim r As BinaryReader = New BinaryReader(fs)
VB
To use the BinaryReader, a number of different Read methods are available to interpret the binary information being read from your chosen stream. This example deals with strings of information, so you will be using the ReadString method, but the BinaryReader exposes other readers such as ReadBoolean, or ReadChar for intepreting other data types. When reading information from your stream, use the PeekChar method to determine when you have reached the end of your stream (in this case, the end of the file). PeekChar returns a negative value when you have reached the end of the stream, as in the following sample.
' make an appropriate receptacle for the information being read in... ' a StringBuilder is a good choice Dim output as StringBuilder = New StringBuilder() ' set the file pointer to the beginning of our file... r.BaseStream.Seek(0, SeekOrigin.Begin) ' continue to perform this loop while not at the end of our file... Do While r.PeekChar() > -1 output.Append( r.ReadString() ) ' use ReadString to read from the file Loop VB
Once you have read in the information, you can do whatever you need with it. At some point however, you are probably going to want to write information back out to the file, so we are going to need our BinaryWriter. In this example, you are going to append information to the end of the file using the Seek method, so make sure the pointer to your file is at the end of the file before beginning our write. There are a couple of choices when writing information with the BinaryWriter. You can use the Write method to write any standard form of information to the stream your writer wraps, since the Write method has plenty of overloads for all the kinds of information you can write. In this situation, you can also use the WriteString method to write a length-prefixed string to the stream. This example uses the Write method. Note that the Write method does accept a string.
' set the file pointer to the end of our file...
w.BaseStream.Seek(0, SeekOrigin.End)
w.Write("Putting a new set of entries into the binary file..." & chr(13))
Dim i As Integer
For i = 0 To 5 Step 1 ' some arbitrary information to write to the file
w.Write( i.ToString() )
Next i
VB
Having demonstrated the basics of reading and writing binary information, it is always a good idea to open the file you have created with an an application such as Microsoft Word or notepad, just to see what the file looks like. You will notice that the file is not human-readable, since the information gets transformed by the writing process to a form which is easier for the computer to use.
|