BlackWaspTM
Input / Output
.NET 2.0+

Writing to Text Files with StreamWriter (2)

One of the simplest, yet very flexible, manners for storing information is within a text file. Such a file allows the storage of data in a human-readable and easily edited format. You can create text files using the .NET framework's StreamWriter class.

Closing the File

Once all of the required information has been written to the file, you should call the StreamWriter's Close method. This ensures that any buffered information is sent to the stream and frees any resources that are in use. Once the StreamWriter is closed, the text file will be complete.

writer.Close();

If you have added all of the above code to the Main method of the console application, run the program now. The file should be created and will contain the following text:

Line 1
Line 2
Line 3

Disposing

The StreamWriter class implements the IDisposable interface to enable the resources that it allocates to be freed on demand, or when the garbage collector destroys the object. When you call the Close method, this actually calls Dispose automatically. When you have finished working with a StreamWriter you can call Dispose instead of Close. This gives the possibility of containing the StreamWriter calls within a using statement, making the code more elegant and ensuring that the object will be disposed correctly. To do so, replace all of the code in the Main method with the following:

using (StreamWriter writer = new StreamWriter(@"c:\temp\test.txt"))
{
    writer.WriteLine("Line 1");
    writer.Write("Line ");
    writer.WriteLine("2");
    writer.WriteLine("Line {0}", 3);
}

Appending to a Text File

Often you will need to append text to an existing file, rather than simply overwriting it. An overloaded version of the StreamWriter constructor allows you to specify whether you wish to overwrite or append by adding a Boolean parameter. If the parameter is set to true, text sent to the StreamWriter will be added at the end of the existing information. If set to false, the file will be replaced. In either case, if the specified file does not exist it will be created.

To reopen the file and append information to it, modify the constructor line as follows. Run the program and review the file's contents to see the results.

using (StreamWriter writer = new StreamWriter(@"c:\temp\test.txt", true))

Text Encoding

The last example in this article allows you to change the encoding method for the text written to the file. By default, the StreamWriter uses UTF-8 encoding. If you need to use an alternative standard, you can specify a third parameter that accepts an object of the Encoding class, from the System.Text namespace. This class includes several static properties that provide other encoding formats.

To demonstrate, modify the constructor as follows to specify that the StreamWriter should use Unicode text:

using (StreamWriter writer = new StreamWriter(@"c:\temp\test.txt", true, Encoding.Unicode))

This completes the article. In the next article I will describe how to read information from text files using the StreamReader class.

16 September 2009