Text File Input and Output with StreamReader and StreamWriter

The StreamReader and StreamWriter classes are specially designed to represent character streams and contain members that make reading from and writing to text files convenient. These streams can only be used with text files. A few of their methods are shown in Tables 22.3 and 22.4.

Both stream classes contain a method called Close that closes the related stream. This releases the resources allocated to the stream and allows other parts of the program access to the underlying file (usually only one stream can access a file at any one time). Furthermore, a stream that is left unclosed without an associated running program is prone to be damaged.

Even if you don't close a stream explicitly ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.