Browse DevX
Sign up for e-mail newsletters from DevX


Take Advantage of Streams and Formatters in VB.NET  : Page 3

.NET File I/O operations use Stream and Formatter classes that abstract many tasks you had to code manually in classic VB.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

As you can see, converting data to and from byte arrays is cumbersome. To avoid the conversions and simplify your code, you can use the StreamReader/StreamWriter classes to access text files, and the BinaryReader/BinaryWriter classes to access binary files. The BinaryReader/BinaryWriter classes derive from the Stream class, because they write binary data (bytes) to an underlying stream, but the StreamReader/ StreamWriter classes derive from the TextReader/TextWriter classes respectively, and perform byte encoding conversions automatically.

To read data from a binary file, create an instance of the BinaryReader class. The BinaryReader class's constructor accepts one argument—a FileStream object representing the file you want to open. You obtain the FileStream by building on the ways you've already seen to open a file, such as the File.OpenRead or File.OpenWrite methods:

Dim BR As New IO.BinaryReader(IO.File.OpenRead(path))

The syntax for the BinaryWriter class's constructor is similar:

Dim BW As New IO.BinaryWriter(IO.File.OpenWrite(path))

The BinaryWriter class exposes Write and WriteLine methods. Both methods accept any of the basic data types as arguments and write the data to the file (the WriteLine method appends a newline character to the end of the data). The BinaryReader class exposes numerous methods for reading data back. The class stores data values in their native format, with no indication of their type, so the program that reads them back should use the appropriate overloaded Read method. The following statements assume that BW is a properly initialized BinaryWriter object, and show how you might write a string, an integer, and a double value to a file:

BW.WriteLine("A String") BW.WriteLine(12345) BW.WriteLine(123.456789999999)

To read the values back, you must use the appropriate methods of a properly initialized BinaryReader object:

Dim s As String = BR.ReadString() Dim i As Int32 = BR.ReadInt32() Dim dbl As Double = BR.ReadDouble()

To access text files, use the StreamReader/StreamWriter classes. The methods are nearly identical. To write text to a file, use either the Write or the WriteLine method. To read the data back, use the Read, ReadLine or ReadToEnd methods. The Read method reads a single character from the stream, ReadLine reads the next text line (up to a carriage-return/line-feed) and ReadToEnd reads all the characters to the end of the file.

You can find more examples of reading from and writing to binary and text files in the section of this article entitled Common File I/O Scenarios.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date