The StreamWriter class is the channel through which you send data to a text file. To create a new StreamWriter object, declare a variable of the StreamWriter type. The first overloaded form of the constructor accepts a file’s path as an argument and creates a new StreamWriter object for the file:
Dim SW As New StreamWriter(path)
Code language: PHP (php)
The new object has the default encoding and the default buffer size. The encoding scheme determines how characters are saved (the default encoding is UTF-8), and the buffer size determines the size of a buffer where data are stored before they’re sent to the file. The following statement creates a new StreamWriter object and associates it with the specified file:
Dim SW As New StreamWriter("c:\TextFile.txt")
Code language: PHP (php)
Another form of the same constructor creates a new StreamWriter object for the specified file by using the default encoding and buffer size, but it allows you to overwrite existing files. If the overwrite argument is True, you can overwrite the contents of an existing file.
Dim SW As New StreamWriter(path, overwrite)
Code language: PHP (php)
You can also specify the encoding for the StreamWriter with the following form of the constructor:
Dim SW As New StreamWriter(path, overwrite, encoding)
Code language: PHP (php)
The last form of the constructor that accepts a file’s path allows you to specify both the encoding and the buffer size:
Dim SW As New StreamWriter(path, overwrite, encoding, bufferSize)
Code language: PHP (php)
The same forms of the constructor can be used with a FileStream object. The simplest form of its constructor is as follows:
Dim SW As New StreamWriter(stream)
Code language: PHP (php)
This form creates a new StreamWriter object for the FileStream specified by the stream argument. To use this form of the constructor, you must first create a new FileStream object and then use it to instantiate a StreamWriter object:
Dim FS As FileStream
FS = New FileStream("C:\TextData.txt", FileMode.Create)
Dim SW As StreamWriter
SW = New StreamWriter(FS)
Code language: PHP (php)
Finally, there are two more forms of the StreamWriter constructor that accept a FileStream object as the first argument. These forms are simply listed here:
New StreamWriter(stream, encoding)
New StreamWriter(stream, encoding, bufferSize)
Code language: PHP (php)
After you have created the StreamWriter object, you can call its members to manipulate the underlying file. They are described in the following sections.
NewLine Property
The StreamWriter object provides a handy property, the NewLine property, which allows you to change the string used to terminate each line in the file. This terminator is written to the text file by the WriteLine method, following the text. The default line-terminator string is a carriage return followed by a line feed (\r\n). The StreamReader object doesn’t provide a similar property. It reads lines terminated by the carriage return (\r), line feed (\n), or carriage return/line feed (\r\n) characters only.
Methods
To send information to the underlying file, use the following methods of the StreamWriter object.
AutoFlush
This property is a True/False value that determines whether the methods that write to the file (the Write and WriteString methods) will also flush their buffer. If you set this property to False, the buffer will be flushed when the operating system gets a chance, when the Flush method is called, or when you close the FileStream object. When AutoFlush is True, the buffer is flushed with every write operation.
Close
This method closes the StreamWriter object and releases the resources associated with it to the system. Always call the Close method after you finish using the StreamWriter object. If you have created the StreamWriter object on top of a FileStream object, you must also close the underlying stream too.
Flush
This method writes any data in the buffer to the underlying file.
Write(data)
This method writes the value specified by the data argument to the Writer object on which it’s applied. The Write method is overloaded and can accept any data type as an argument. When you pass a numeric value as an argument, the Write method stores it to the file as a string. This is the same string you’d get with the number’s ToString method. To save dates to a text file, you must convert them to strings with one of the methods of the Date data type.
There’s one form of the Write method I want to discuss here. This overloaded form accepts a string with embedded format arguments, followed by a list of values, one for each argument. The following statement writes a string with two embedded numeric values in it, as shown in the following line:
SW.Write("Your price is ${0} plus ${1} for shipping", 86.50, 12.99)
Your price is $86.50 plus $12.99 for shipping
Code language: JavaScript (javascript)
WriteLine(data)
This method is identical to the Write method, but it appends a line break after saving the data to the file. You will find examples on using the StreamWriter class after we discuss the methods of the StreamReader class.