C# Using TextWriter

File image with lines of text

TextWriter creates text output. You want to see an example of the TextWriter class in the C# language and the .NET Framework for writing text to files without a specific encoding requirement. The TextWriter class is used to implement other file classes in the base class library, and you can also use it to implement lower-level file IO logic in your higher-level classes.

This C# example program uses the TextWriter class. It writes an output file.

Note: It's typically better to use StreamWriter in your C# programs.

Example

First here we look at the TextWriter class, which we instantiate from the result of the File.CreateText method in the System.IO namespace. The TextWriter class provides useful methods for writing lines and text to a file. The TextWriter class is easiest to use reliably when you wrap it in a using statement and a resource acquisition statement, which generates internal logic for handling unmanaged resources like file handles.

Program that uses TextWriter [C#]

using System.IO;

class Program
{
    static void Main()
    {
	//
	// Create a new TextWriter in the resource acquisition statement.
	//
	using (TextWriter writer = File.CreateText("C:\\perl.txt"))
	{
	    //
	    // Write one line.
	    //
	    writer.WriteLine("First line");
	    //
	    // Write two strings.
	    //
	    writer.Write("A ");
	    writer.Write("B ");
	    //
	    // Write the default newline.
	    //
	    writer.Write(writer.NewLine);
	}
    }
}

File created by program
    (Located at C:\perl.txt)

First line
A B
 
Using keyword

Description. The program uses the using statement in the C# programming language, which provides a way to efficiently and reliably handle unmanaged file resources. The using statement has a resource acquisition statement, which specifies a read-only variable that is only available in the enclosed statement list.

Calling WriteLine and Write methods. In the statement list of the using statement block, the program uses the WriteLine instance method and the Write instance method on the TextWriter instance we allocated. The WriteLine writes the parameter string to the file and automatically adds a newline sequence after that string. The Write method does not add the newline sequence. If you are writing strings that already have line terminations, use the Write method.

NewLine property. The program also uses the NewLine instance property on the TextWriter class. You can assign or read this property if you want to. By default, it will be set to the platform's default newline sequence, which on Windows is "\r\n".

Environment.NewLine

StreamWriter

Programming tip

The StreamWriter class actually inherits from the TextWriter class in the base clas library. The TextWriter class is an abstract base class, which means it provides functionality for StreamWriter. MSDN states that StreamWriter represents writing in a particular encoding. For this reason, unless you have specialized encoding requirements, StreamWriter is more appropriate because it manages the encoding for you.

Using StreamWriter

Summary

The C# programming language

We looked at the TextWriter class in the C# programming language and related it to the StreamWriter class by noting that it is used as a base class. The TextWriter class is lower-level than StreamWriter because it does not handle encodings automatically for you. But it is still useful in many cases when you have specific encoding requirements or always want to use ASCII encoding.

File Handling
.NET