TextReader Class
Represents a reader that can read a sequential series of characters. TextReader is an abstract class.
Overview
The TextReader class is the abstract base class for all readers that read characters from a source. It provides a common set of methods for reading characters, strings, and lines from various input sources, such as files, streams, or strings.
Common derived classes include:
- StreamReader: Reads characters from a stream.
- StringReader: Reads characters from a string.
Members
Methods
- Peek(): Returns the next character without consuming it from the input stream.
-
public abstract int Peek();
Returns an integer representing the next character to be read, or -1 if there are no more characters or if the stream does not support seeking.
- Read(): Reads the next character from the input stream and advances the character position.
-
public abstract int Read();
Returns an integer representing the character read, or -1 if there are no more characters.
- Read(char[] buffer, int index, int count): Reads a specified maximum number of characters into an array at the specified offset, beginning and ending when all characters are read or when the end of the stream is reached.
-
public virtual int Read(char[] buffer, int index, int count);
Copies the characters read into the
buffer
. Returns the number of characters read. If the end of the stream is reached before all characters are read, the return value is the number of characters read. If all characters are read, the return value is the number of characters read. - ReadLine(): Reads the next line of characters from the current stream and returns data as a string.
-
public virtual string ReadLine();
Returns the next line of characters from the reader. Returns
null
if the end of the stream is reached. - ReadToEnd(): Reads all characters from the current position to the end of the stream and returns them as one string.
-
public virtual string ReadToEnd();
Returns a string containing all characters from the current position to the end of the stream.
Properties
- (implicit) : Represents a textual reader that can read characters from various sources.
-
The TextReader class provides methods for reading text data, such as characters, strings, and lines. It serves as an abstract base class for other text-reading classes like StreamReader and StringReader.
Example Usage (StreamReader)
using System;
using System.IO;
public class Example
{
public static void Main(string[] args)
{
string filePath = "my_document.txt";
// Create a dummy file for demonstration
if (!File.Exists(filePath))
{
File.WriteAllText(filePath, "This is line one.\nThis is line two.\nAnd the final line.");
}
try
{
// Use StreamReader which derives from TextReader
using (TextReader reader = new StreamReader(filePath))
{
string line;
Console.WriteLine("Reading file line by line:");
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
Console.WriteLine("\nReading entire content:");
// Reset stream position to the beginning for ReadToEnd
reader.BaseStream.Seek(0, SeekOrigin.Begin);
string allContent = reader.ReadToEnd();
Console.WriteLine(allContent);
}
}
catch (FileNotFoundException)
{
Console.WriteLine($"Error: The file '{filePath}' was not found.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}