Grasp the Stream in VB. NET (2)

Source: Internet
Author: User
Tags object serialization
Grasp the Stream in VB. NET (2)

  Flexible and diverse IO OperationSometimes conversion between data and byte arrays is cumbersome. To avoid these boring conversions and simplified code, it is wise to use streamreader/streamwrite and binaryreader/binarywriter. Streamreader and streamwrite are derived from the textreader and textwriter classes respectively, and are automatically converted to Byte encoding. Binaryreader/binarywriter is derived from stream and mainly reads and writes data in binary format. When reading data from a binary file, first create a binaryreader instance. The binaryreader construction function accepts a filestream object, which indicates the file to be read. We have seen it before and can use File. OpenReadOr File. OpenWriteMethod To create a filestream object. Dim BR as new IO. binaryreader (Io. file. openread (PATH) dim BW as new IO. binarywriter (Io. file. openwrite (PATH) binarywriter class has WriteAnd WriteLineEither method can accept any type of data as a parameter to write data to a file ( WriteLineAppend a row of data at the end of the file ). The BinaryReader class has many data reading methods. when the data is stored in a file, there is no information about its own type. Therefore, you must select an appropriate method to reload the data. The following example assumes that BW is an initialized BinaryWriter object, indicating how to write a string, integer, and double-precision number to the file: BW. writeLine ("A String") BW. writeLine (1, 12345) BW. when reading data back from WriteLine (123.456789999999), You must select the proper Read method of BinaryReader: Dim s As String = BR. readString () Dim I As Int32 = BR. readInt32 () Dim dbl As Double = BR. readDouble () uses StreamReader/StreamWriter objects for text files. The method is similar to the above, and the Write and WriteLine methods are also used to Write data. The Read method reads a single character, ReadLine reads a row of data (until there is a carriage return/line break), ReadToEnd reads all the characters, and ends with the end of the file.

Object serializationSo far, we have only written simple data to a file and read it back to the program. In fact, the data read and written by most programs may not be a simple type but a complex structure, such as arrays, array lists, and hash tables. Therefore, we adopt a serialization technology, first converting the value of the array into a byte sequence, and then writing the file, so that the entire array is stored. On the contrary, we call it deserialization. Serialization is a big topic of. NET. This column describes the basic information. BinaryFormatterSerializeAndDeserializeMethod to save an object to a file and read it back to the program. First, imports System. RunTime. Serialization. Formatters, so as not to write so long statements. The Formatters namespace contains the BinaryFormatter class, which is used to serialize objects in binary data. Create a BinaryFormatter instance and then callSerializeMethod,SerializeTwo parameters are accepted: one is a writable FileStream instance, which is used to store data files; 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) BinaryFormatter'sDeserializeThe method has only one parameter, FileStream instance. At the current position, deserialization produces an object of unknown type. We must use Ctype to convert it to the original object. The following example deserializes the above file to obtain the original Rectangle object: Dim R As New Rectangle () R = CType (BinFormatter. deserialize (FS), Rectangle) We can also serialize objects with XmlFormatter. First, select Add System in the IDE Project menu. runtime. serialization. formatters. soap, and then you can create a SoapFormatter object. The method is the same As BinFormatter, but the data storage adopts the 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) opens c:/Rect. xml, which actually stores the following content:

-<SOAP-ENV: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: SOAP-ENC = "http://schemas.xmlsoap.org/soap/encoding/" xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: CLR = "http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV: encodingstyle = "http://schemas.xmlsoap.org/soap/encoding/">
-SOAP-ENV: Body>
-<A1: rectangle id = "ref-1" xmlns: A1 = "http://schemas.microsoft.com/clr/nsassem/System.Drawing/System.Drawing%2C%20Version%3D1.0.3300.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Db03f5f7f11d50a3a">
<X> 8 </x>
<Y> 8 </Y>
<Width> 249 </width>
<Height> 499 </A1: rectangle>
SOAP-ENV: Body>
SOAP-ENV: envelope>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.