Export (0) Print
Expand All
2 out of 8 rated this helpful - Rate this topic

TextReader Class

Represents a reader that can read a sequential series of characters.

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

System.Object
   System.MarshalByRefObject
      System.IO.TextReader
         System.IO.StreamReader
         System.IO.StringReader

[Visual Basic]
<Serializable>
MustInherit Public Class TextReader
   Inherits MarshalByRefObject
   Implements IDisposable
[C#]
[Serializable]
public abstract class TextReader : MarshalByRefObject, IDisposable
[C++]
[Serializable]
public __gc __abstract class TextReader : public
   MarshalByRefObject, IDisposable
[JScript]
public
   Serializable
abstract class TextReader 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

TextReader is the abstract base class of StreamReader and StringReader, which read characters from streams and strings, respectively. Create an instance of TextReader to open a text file for reading a specified range of characters, or to create a reader based on an existing stream. You can also use an instance of TextReader to read text from a custom backing store using the same APIs you would use for a string or a stream.

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

A derived class must minimally implement the Peek and Read methods in order to make a useful instance of TextReader.

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

TextReader 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.