The streamreader object is used to read files, and the streamwriter object is used to write files. The attributes and methods of streamwriter and streamreader are similar.
1. Create a streamwriter object
To create a streamwriter object, you can use the createtext method of the file object or the constructor of the streamwriter object.
(1) Use the createtext method of the file object
Dim mikecatwriter as streamwriter
Dim mikecatfile as file
Mikecatwriter = mikecatfile. createtext ("c: \ mikecat.txt ")
(2) Use the constructor of the streamwriter object
Dim mikecatwriter as new streamwriter ("c: \ mikecat.txt") 2. Main method of streamwriter object
The streamwriter object writes data to a file. The following describes the specific operation methods.
(1) close Method
Like the streamreader object, the close method is also a required method for streamwriter objects. After the streamwriter object is used to complete the write operation, you must use the close method to release the files operated by the streamwriter object and other system resources. If the close method is not used, other processes cannot perform any operations on the file.
Sub close ()
(2) write Method
This method is used to write data to a file.
Sub write (string)
The string parameter can be a number, character, or string. Both characters and numbers are written into the file as strings.
Writeline Method
Corresponds to the readline method of the streamreader object. This method is used to write a row of data.
Sub writeline (string)
The string parameter can be a number, character, or string. Both characters and numbers are written into the file as strings.