Export (0) Print
Expand All
5 out of 7 rated this helpful - Rate this topic

TextWriter Class

Represents a writer that can write a sequential series of characters. This class is abstract.

For a list of all members of this type, see TextWriter Members.

System.Object
   System.MarshalByRefObject
      System.IO.TextWriter
         System.CodeDom.Compiler.IndentedTextWriter
         System.IO.StreamWriter
         System.IO.StringWriter
         System.Web.HttpWriter
         System.Web.UI.HtmlTextWriter

[Visual Basic]
<Serializable>
MustInherit Public Class TextWriter
   Inherits MarshalByRefObject
   Implements IDisposable
[C#]
[Serializable]
public abstract class TextWriter : MarshalByRefObject, IDisposable
[C++]
[Serializable]
public __gc __abstract class TextWriter : public
   MarshalByRefObject, IDisposable
[JScript]
public
   Serializable
abstract class TextWriter extends MarshalByRefObject
   implements IDisposable

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

TextWriter is the abstract base class of StreamWriter and StringWriter, which write characters to streams and strings, respectively. Create an instance of TextWriter to write an object to a string, write strings to a file, or to serialize XML. You can also use an instance of TextReader to write text to a custom backing store using the same APIs you would use for a string or a stream, or to add support for text formatting.

All the Write methods of TextWriter having primitive data types as parameters write out the values as strings.

By default, a TextWriter is not thread safe. See TextWriter.Synchronized for a thread-safe wrapper.

A derived class must minimally implement the TextWriter(Char) method in order to make a useful instance of TextWriter.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File

Example

[Visual Basic, C#, C++] The following code example demonstrates the polymorphic behavior of the TextReader and TextWriter types. Since aStringWriter and aStreamWriter are both TextWriter types, the WriteVowel method is called with both objects and the Write methods associated with each specific type are executed. Similarly, the ReadText method is called with both aStringReader and aStreamReader, and the correct ReadToEnd method is executed. Note that for aStringWriter and aStringReader, the backing store is a string, while a file is the backing store for aStreamWriter and aStreamReader.

[Visual Basic] 
Imports System
Imports System.IO

Public Class TextRW

    Shared Sub Main()
        Dim aStringWriter, aStreamWriter As TextWriter
        aStringWriter = New StringWriter()
        aStreamWriter = New StreamWriter("InvalidPathChars.txt")
        
        WriteText(aStringWriter)
        WriteText(aStreamWriter)
        aStreamWriter.Close()
        
        Dim aStringReader, aStreamReader As TextReader
        aStringReader = New StringReader(aStringWriter.ToString())
        aStreamReader = New StreamReader("InvalidPathChars.txt")
        
        ReadText(aStringReader)
        ReadText(aStreamReader)
        aStreamReader.Close()
    End Sub

    Shared Sub WriteText(aTextWriter As TextWriter)
        aTextWriter.Write("Invalid file path characters are: ")
        aTextWriter.Write(Path.InvalidPathChars)
        aTextWriter.Write("."C)
    End Sub

    Shared Sub ReadText(aTextReader As TextReader)
        Console.WriteLine("From {0} - {1}", _
            aTextReader.GetType().Name, aTextReader.ReadToEnd())
    End Sub

End Class

[C#] 
using System;
using System.IO;

class TextRW
{
    static void Main()
    {
        TextWriter stringWriter = new StringWriter();
        using(TextWriter streamWriter = 
            new StreamWriter("InvalidPathChars.txt"))
        {
            WriteText(stringWriter);
            WriteText(streamWriter);
        }

        TextReader stringReader = 
            new StringReader(stringWriter.ToString());
        using(TextReader streamReader = 
            new StreamReader("InvalidPathChars.txt"))
        {
            ReadText(stringReader);
            ReadText(streamReader);
        }
    }

    static void WriteText(TextWriter textWriter)
    {
        textWriter.Write("Invalid file path characters are: ");
        textWriter.Write(Path.InvalidPathChars);
        textWriter.Write('.');
    }

    static void ReadText(TextReader textReader)
    {
        Console.WriteLine("From {0} - {1}", 
            textReader.GetType().Name, textReader.ReadToEnd());
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

static void WriteText(TextWriter* textWriter)
{
    textWriter->Write("Invalid file path characters are: ");
    textWriter->Write(Path::InvalidPathChars);
    textWriter->Write(Char::Parse(S"."));
}

static void ReadText(TextReader* textReader)
{
    Console::WriteLine(S"From {0} - {1}", 
        textReader->GetType()->Name, textReader->ReadToEnd());
}

void main()
{
    TextWriter* stringWriter = new StringWriter(); 
    TextWriter* streamWriter = 
        new StreamWriter("InvalidPathChars.txt");

    WriteText(stringWriter);
    WriteText(streamWriter);
    streamWriter->Close();

    TextReader* stringReader = 
        new StringReader(stringWriter->ToString()); 
    TextReader* streamReader = 
        new StreamReader("InvalidPathChars.txt");

    ReadText(stringReader);
    ReadText(streamReader);
    streamReader->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.IO

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

TextWriter Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File | Basic File I/O | Reading and Writing to a Newly Created Data File

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.