Grasp the stream in VB.NET (2)
Flexible and diverse IO operation
Sometimes, the conversion between the data and byte arrays is a cumbersome thing. In order to avoid these bored conversion and simplifying the code, StreamReader / streamwrite and binaryreader / binarywriter are not wise. StreamReader / streamWrite is derived by the TextReader / TextWriter class, automatically performs the conversion of byte encoding. BinaryReader / BinaryWriter is derived from stream, mainly read and write data in binary form.
When reading data from binary file, first create a BinaryReader instance, BinaryReader's build function accepts a FileStream object, which represents the file to be read. We have already seen before we can create a FileStream object with File.OpenRead or File.OpenWrite methods.
As follows:
Dim br as new o.binaryreader (IO.FILE.Openread (PATH))
DIM BW As new Io.binaryWriter (IO.FILE.OpenWrite (PATH))
BinaryWriter class has two methods of WRITE and WRITELINE, can accept any type of data as a parameter write file (WriteLine adds a line of data at the file). The BinaryReader class has a lot of read data. When the data is stored on the file, there is no information about the type of information, so when reading the data, the appropriate overloaded Read method must be selected.
The following example assumes that BW is a binarywriter object that has been initialized, indicating how to write a string, integer, double precision number to file:
BW.WRITELINE ("a string")
BW.WRITELINE (12345)
Bw.writeline (123.456789999999)
When reading back the data, you must choose a BinaryReader suitable Read method:
DIM S as string = br.ready 4tring ()
DIM I as int32 = br.readint32 ()
DIM DBL AS DOUBLE = Br.Readdouble ()
For text files, the StreamReader / StreamWriter object is used. The method is similar to the above, and write data is also used by WRITE and WRITELINE methods. Readline reads a character, readline reads a line of data (until the Enter / Finite is available), readtoend reads all characters, and ends to the file.
Object sequence
So far, we just write simple type data to the file and read back the program. In fact, most programs read and written may not be simple type, but complex structures, such as: arrays, array lists, hash tables, etc. So, we take a sequential technology that first transforms the value of the array into a byte sequence, then writes the file, so that the entire array is stored. Instead, we call it anti-sequence.
Serialization is a big topic for .NET, this column introduces basic information.
Save an object to the file and read the program with binary systematter's Serialize and DeSerialize methods. First, Imports System.Runtime.Serialization.Formatters can be removed so long. The Formatters name space contains a BinaryFormatter class for use in binary data serialization.
Create a BinaryFormatter instance, then call the serialize method, serialize accepts two parameters: one is a writable FileStream instance for saving the file; the other is the object itself:
DIM BINFORMATTER As new binary.binaryformatter ()
DIM R AS New Rectangle (10, 20, 100, 200)
Binformatter.Serialize (fs, r)
The DESERIALIZE method of BinaryFormatter has only one parameter, and the FileStream instance. In the current position, the reverse sequence is a type unknown object, we must convert to the original object with CType. The following example reflects the above file to get the original Rectangle object:
DIM R as new rectangle ()
R = ctype (binformatter.deSerialize (fs), rectangle)
We can also serialize in XMLFormatter. First, select Add System.Runtime.Serialization.Formatters.SoAP in the IDE's Project Menu, then you can create a SOAPFormatter object, the method is the same as binformatter, only the data store is used in XML format:
DIM FS AS New IO.FILESTREAM ("C: /Rect.xml", IO.FILEMODE.CREATE, IO.FILEACCESS.WRITE
DIM XMLFORMATTER AS New SOAPFORMATTER ()
DIM R AS New Rectangle (8, 8, 299, 499)
XMLFormatter.Serialize (fs, r)
Open C: /Rect.xml, actually stores these contents:
-
-
-
a1: Rectangle>
Soap-env: body> soap-env: envelope>