System.IO.StreamReader Class

Implements a System.IO.TextReader that reads characters from a byte stream in a particular encoding.

See Also: StreamReader Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class StreamReader : TextReader

Remarks

System.IO.StreamReader is designed for character input in a particular encoding, whereas the System.IO.Stream class is designed for byte input and output. Use System.IO.StreamReader for reading lines of information from a standard text file.

System.IO.StreamReader defaults to UTF-8 encoding unless specified otherwise, instead of defaulting to the ANSI code page for the current system. UTF-8 handles Unicode characters correctly and provides consistent results on localized versions of the operating system. If you get the current character encoding using the StreamReader.CurrentEncoding property, the value is not reliable until after the first StreamReader.Read method, since encoding auto detection is not done until the first call to a StreamReader.Read method.

By default, a System.IO.StreamReader is not thread safe. See TextReader.Synchronized(TextReader) for a thread-safe wrapper.

The StreamReader.Read(Char[], int, int) and StreamWriter.Write(Char[], int, int) method overloads read and write the number of characters specified by the count parameter. These are to be distinguished from BufferedStream.Read(Byte[], int, int) and BufferedStream.Write(Byte[], int, int), which read and write the number of bytes specified by the count parameter. Use the System.IO.BufferedStream methods only for reading and writing an integral number of byte array elements.

Note:

When reading from a System.IO.Stream, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.

For a list of common I/O tasks, see Common I/O Tasks.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0