Wednesday, November 3, 2010

StreamWriter object

HOW TO: Declare a StreamWriter object
Syntax
{Dim | Private} streamWriterVariableName As IO.StreamWriter

Example
Dim outFile As IO.StreamWriter
declares a StreamWriter variable named outFile

HOW TO: Create a StreamWriter object
Syntax
IO.File.method(fileName)

Methods
CreateText: opens a file for output - creates a new empty file to which data can be written. If the file already exists, the data in the file is deleted before any new data is written to it.

AppendText: opens a file for append - the new data is written after any existing data in the file. If the file does not exist, the file is created for you.

Example 1
outFile = IO.File.CreateText(“mytextfile.txt”)
opens the mytextfile.txt file for output; creates a StreamWriter object and assigns it to the outFile variable. The file is open for output.

Example 2
outFile = IO.File.AppendText(“report.txt”)
opens the report.txt file for append; creates a StreamWriter object and assigns it to the outFile variable. The file is open for append.

No comments:

Post a Comment