.NET API Documentation

System.IO.StreamReader Class

StreamReader Class

Represents a text reader that reads characters from a byte stream in a particular encoding.

This class is designed for reading streams of characters, typically text files. It handles character encoding and buffer management to efficiently read lines and individual characters.

Namespace: System.IO

Assembly: System.Runtime.IO.dll

Inheritance

System.Object

System.MarshalByRefObject

System.IO.TextReader

System.IO.StreamReader

Constructors

StreamReader(Stream, Encoding)

Initializes a new instance of the StreamReader class for the specified stream and encoding. Reads from the stream until the end is reached.

Parameters:

  • stream: The stream to read from.
  • encoding: The character encoding to use.

using System.IO;
using System.Text;

// Example usage (assuming 'myStream' is an opened Stream)
// StreamReader reader = new StreamReader(myStream, Encoding.UTF8);

StreamReader(string)

Initializes a new instance of the StreamReader class for the specified file path. The current file or the UTF-8 with signature (BOM) encoding is used.

Parameters:

  • path: The complete file path to read.

// Example usage
// StreamReader reader = new StreamReader("myFile.txt");

Methods

ReadLine()

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

Returns: The next line of characters from the stream, or null if the end of the stream has been reached.


// Example usage
// string line = reader.ReadLine();
// while (line != null) {
//     Console.WriteLine(line);
//     line = reader.ReadLine();
// }

ReadToEnd()

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

Returns: A string containing all characters from the current position to the end of the stream.


// Example usage
// string allText = reader.ReadToEnd();
// Console.WriteLine(allText);

Peek()

Returns the next character to be read without actually consuming it or the next byte in the stream.

Returns: The next character, or -1 if no more characters are available or if the stream does not support seeking.

Read()

Reads the next character from the input stream and advances the character position by one.

Returns: The next character, or -1 if no more characters are available.

Properties

EndOfStream

Gets a value indicating whether the StreamReader has reached the end of the stream.

Returns: true if the end of the stream has been reached; otherwise, false.

CurrentEncoding

Gets the character encoding used by the StreamReader object.

Returns: The Encoding object used by the StreamReader.

BaseStream

Gets the underlying stream.

Returns: The Stream that the StreamReader is wrapping.

Remarks

Use the StreamReader class to read from any stream that contains bytes that can be decoded into characters. The StreamReader can read characters, lines, and all characters from a stream.

It is recommended to use the StreamReader over using FileStream and then converting the bytes to characters yourself, as StreamReader handles character encoding and buffering more effectively.

See Also