Grasp the stream in VB.NET (STREAM) (3)

zhaozj2021-02-17  73

Grasp the stream in VB.NET (STREAM) (3)

Document operation specific example

In this section, you will find more commonly used code instances. The most commonly used, the most basic operation is to write Text into files and read back. The current application usually does not have a binary file to store simple variables, and it is used to store objects, object gathers, and other machine code. Next, an example of the specific operation will be seen.

Read and write text files

In order to save Text to the file, create a FileStream-based StreamReader object, and then call the Write method to write the text required to save. The following code is prompted by SaveFileDialog prompting the user to specify a file to save the contents of TextBox1.

SavefileDialog1.filter = _

"Text Files | * .txt | all files | *. *"

SaveFileDialog1.FilterIndex = 0

If SavefileDialog1.ShowDialog = DialogResult.ok Then

DIM fs as filestream = savefiledialog1.openfile

DIM SW AS New StreamWriter (fs)

SW.write (TextBox1.text)

SW.CLOSE ()

Fs.close ()

END IF

Similarly, similar statements, we read a text file and display the content in the TextBox control. StreamReader's readtoEnd method returns all the contents of the file.

OpenFileDialog1.filter = _

"Text Files | * .txt | all files | *. *"

OpenFileDialog1.FilterIndex = 0

If OpenFileDialog1.ShowDialog = DialogResult.ok then

DIM FS AS FileStream

FS = OpenFileDialog1.openfile

DIM SR AS New StreamReader (FS)

TextBox1.text = sr.readToeD

Sr.close ()

Fs.close ()

END IF

Storage of various objects

A specific object can be serially used in a binary form in binary form, or using the SOAPFormatter class in an XML format. As long as you change all binaryformatter to SOAPFormatter, you can serialize the object in an XML format without changing any code.

First create a BinaryFormatter instance:

DIM BINFORMATTER As new binary.binaryformatter ()

Then create a FileStream object for storing sered sequences:

Dim fs as new system.io.filestream ("c: /test.txt", IO.FILEMODE.CREATE

Then call the BinFormatter's Serialize method serialized any Framework object that can be serialized:

R = new Rectangle (rnd.next (0, 100), rnd.next (0, 300), _

Rnd.next (10, 40), Rnd.Next (1, 9))

Binformatter.Serialize (fs, r)

Add a serializable property to make custom objects can be serialized

public structure person

DIM Name as String

DIM AGE AS INTEGER

DIM INCOME As Decimal

End structure

The following code creates a Person object instance, then call the BINFORMATTER's Serialize method serialized self-defined object: P = new person ()

P.Name = "Joe Doe"

P.AGE = 35

P.income = 28500

Binformatter.Serialize (fs, p)

You can also serialize other objects in the same stream, then read back in the same order. For example, after serializing the Person object, then serialize a Rectangle object:

Binformatter.Serialize (FS, New Rectangle (0, 0, 100, 200)))

Create a BinaryFormatter object, call its Deserialize method, and turn the return value to the correct type, which is the entire reverse sequence process. Then sequence the other objects of the Stream.

Assuming that both of the Person and Rectangle have been seriallyified, in the same order, we can get the original object:

DIM P as new person ()

P = binformatter.Serialize (fs, person)

DIM R AS New Rectangle

R = binformatter.serialize (fs, rectangle)

Persisting Collectes

Collection store

Most programs handle object collections rather than individual objects. For collection data, first create an array (or other types of collection, such as arraylist or hashtable), populate with objects, and then a serialize method can serialize the true collection, is it simple? The following example first creates an ArrayList with two Person objects, then serialize itself:

Dim fs as new system.IO.FileStream_

("C: / Test.txt", IO.FILEMODE.CREATE)

DIM BINFORMATTER As new binary.binaryformatter ()

DIM P as new person ()

DIM PERSONS AS New ArrayList

P = new person ()

P.Name = "Person 1"

P.AGE = 35

P.income = 32000

Persons.Add (P)

P = new person ()

P.Name = "Person 2"

P.AGE = 50

P.income = 72000

Persons.Add (P)

Binformatter.Serialize (fs, person)

With the file of the store serialized data, call a Binary Systematter instance's DeSerialize method, it will return an object and convert it into a suitable type. The following code reverse sequences all objects in the file and processes all Person objects:

FS = new system.io.filestream_

("C: / Test.txt", IO.FILEMODE.OPENORCREATE

DIM OBJ AS Object

DIM P as person (), R as Rectangle ()

DO

Obj = binformatter.deSerialize (fs)

IF Obj.gettype is gettype (person) THEN

P = ctype (obj, person) 'Process the P Object

END IF

Loop while fs.position

Fs.close ()

The following example calls the Deserialize method to reverse sequencing true collections, then convert the return value to the appropriate type (Person):

FS = new system.io.filestream ("c: /test.txt", ousfilemode.openorcreate)

DIM OBJ AS Object

DIM PERSONS AS New ArrayList

Obj = ctype (binformatter.deSerialize (fs), arraylist)

Fs.close ()

转载请注明原文地址:https://www.9cbs.com/read-30882.html

New Post(0)