Description
Members
Example

TextReader Class

Namespace: System.IO
Assembly: System.dll
Represents an abstract base class for all text readers, providing a common, simple contract for reading characters from a sequential source of characters. The TextReader class is the abstract base class for all readers that read strings. The StringReader class is derived from TextReader and reads from a string. The StreamReader class is also derived from TextReader and reads from a stream.

Inheritance

Object → TextReader

Remarks

The TextReader class provides a base class for reading text. It supports reading characters one at a time, a string of characters, or an array of characters. The TextReader class also provides methods for detecting the end of a stream and for closing the reader.

When you derive a class from TextReader, you must override the following abstract members: Read(), Peek(), and ReadBlock(char[] buffer, int index, int count).

The Dispose() method is called automatically when a TextReader is no longer needed. You can override the Dispose(bool disposing) method to perform additional cleanup if necessary.

Members

Fields >
  • Null public static readonly TextReader Null

    Returns a TextReader that has no characters available.

Properties >
  • Disposed protected bool Disposed

    Indicates whether the TextReader object has been disposed of.

  • LineNumber public virtual int LineNumber { get; }

    Gets the current line number.

  • LinePosition public virtual int LinePosition { get; }

    Gets the current line position.

Methods >
  • Close public virtual void Close()

    Closes the TextReader and releases any system resources associated with the reader.

  • Dispose public void Dispose()

    Releases all resources used by the current instance of the TextReader class.

  • Read public abstract int Read()

    Reads the next character and advances the character or stream position.

  • Read public virtual int Read(char[] buffer, int index, int count)

    Reads a maximum of count characters from the current stream and writes them to buffer starting at index. Returns the number of characters read.

  • ReadBlock public virtual int ReadBlock(char[] buffer, int index, int count)

    Reads a block of characters from the current reader and advances the position within the read data.

  • ReadAll public string ReadAll()

    Reads all characters from the current position to the end of the stream and returns them as a string.

  • ReadLine public virtual string ReadLine()

    Reads a line of characters from the current stream and returns the data as a string.

  • ReadPastNewline protected virtual int ReadPastNewline()

    Reads past the next newline character.

  • ReadUrl public string ReadUrl()

    Reads all characters from the current position to the end of the stream and returns them as a string. Deprecated.

  • Peek public abstract int Peek()

    Returns the next character without consuming it or the character or stream position.

  • ConvertToString protected string ConvertToString(char[] buffer)

    Converts a character array to a string.

  • Skip public virtual long Skip(long unicodeCharCount)

    Skips the specified number of characters.

Protected Methods >
  • Dispose protected virtual void Dispose(bool disposing)

    Releases the unmanaged resources used by the TextReader and optionally releases the managed resources.

Example

Reading from a String

This example demonstrates how to use the StringReader (a concrete implementation of TextReader) to read from a string.

using System;
using System.IO;

public class Example
{
    public static void Main()
    {
        string text = "Hello, World!\nThis is a test.";

        using (StringReader reader = new StringReader(text))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
    }
}

Output:

Hello, World!
This is a test.

Reading from a File

This example demonstrates using StreamReader (another implementation of TextReader) to read a file line by line.

using System;
using System.IO;

public class FileReadExample
{
    public static void Main()
    {
        string filePath = "my_document.txt"; // Assume this file exists

        // Create a dummy file for demonstration
        File.WriteAllText(filePath, "First line of the file.\nSecond line.\nThird and final line.");

        try
        {
            using (StreamReader reader = new StreamReader(filePath))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine($"Error: The file '{filePath}' was not found.");
        }
        catch (IOException ex)
        {
            Console.WriteLine($"An I/O error occurred: {ex.Message}");
        }
        finally
        {
            // Clean up the dummy file
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
    }
}

Assumed Content of my_document.txt:

First line of the file.
Second line.
Third and final line.

Output:

First line of the file.
Second line.
Third and final line.