File Operation instanceIn this section, you will find more common code instances for file operations. The most common and basic operation is to write and read the text into a file. Today's applications usually use binary files instead of simple variables to store objects, object sets, and other machine code. The following shows an example of a specific operation.
Read/write text filesTo save text to a file, create a StreamReader object based on FileStream and call the Write method to Write the text to be saved to the file. The following code uses SaveFileDialog to prompt you to specify a file to save the content 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 uses similar statements. We read a text file and display the content in the TextBox Control. The ReadToEnd method of StreamReader returns all the content 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. readToEnd SR. close () FS. close () End If
Persisting Collections
Storage of Collections
Most programs process object sets instead of individual objects. For set data, first create an array (or a set of other types, such as ArrayList or HashTable), fill in the object, and then a Serialize method can Serialize the real set, isn't it easy? In the following example, we first create an ArrayList with two Person objects and 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, Persons)
The Deserialize method of a BinaryFormatter instance is called as a parameter to store serialized data. An object is returned and converted to a suitable type. The following code deserializes all objects in the file and then 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 objext
End if
Loop while fs. Position <fs. Length-1
FS. Close ()
The following example calls the deserialize method to deserialize a set and converts the returned value to a suitable type (person ):
FS = new system. Io. filestream ("C:/test.txt", Io. filemode. openorcreate)
Dim OBJ as object
Dim persons as new arraylist
OBJ = ctype (binformatter. deserialize (FS), arraylist)
FS. Close ()
Storage of various objects
You can use binaryformatte in binary format or use the soapformatter class to serialize a specific object in XML format. You only need to change the reference of all binaryformatter to soapformatter, so you can serialize the object in XML format without changing any code.
First, create a binaryformatter instance:
Dim BinFormatter As New Binary. BinaryFormatter ()
Then create a FileStream object used to store the serialized object:
Dim FS As New System. IO. FileStream ("c:/test.txt", IO. FileMode. Create)
Call the Serialize method of BinFormatter to Serialize any framework objects 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 attribute so that custom objects can be serialized.
<Serializable ()> 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 and calls the Serialize method of BinFormatter to Serialize the custom 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 and read them back in the same order. For example, after the person object is serialized, a rectangle object is serialized:
Binformatter. serialize (FS, new rectangle (0, 0,100,200 ))
Create a binaryformatter object, call its deserialize method, and convert the returned value to the correct type, which is the whole deserialization process. Then other objects of stream are serialized.
Assume that the person and rectangle objects have been serialized. in the same order, we can get the original objects through deserialization:
Dim P as new person ()
P = binformatter. serialize (FS, Person)
Dim R as new rectangle
R = binformatter. serialize (FS, rectangle)