StreamReader Class
Implements a TextReader that reads characters from a byte stream in a particular encoding.
For a list of all members of this type, see StreamReader Members.
System.Object
System.MarshalByRefObject
System.IO.TextReader
System.IO.StreamReader
[Visual Basic] <Serializable> Public Class StreamReader Inherits TextReader [C#] [Serializable] public class StreamReader : TextReader [C++] [Serializable] public __gc class StreamReader : public TextReader [JScript] public Serializable class StreamReader extends TextReader
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
StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.
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.
By default, a StreamReader is not thread safe. See TextReader.Synchronized for a thread-safe wrapper.
StreamReader.Read(Char[], Int32, Int32) and StreamWriter(Char[], Int32, Int32) read and write the number of characters specified by the count parameter. These are to be distinguished from BufferedStream.Read and BufferedStream.Write, which read and write the number of bytes specified by the count parameter. Use the BufferedStream methods only for reading and writing an integral number of byte array elements.
Note When reading from a Stream, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.
For an example of using this class, see the Example section below. 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 |
| 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 example uses an instance of StreamReader to read text from a file.
[Visual Basic] Imports System Imports System.IO Class Test Public Shared Sub Main() Try ' Create an instance of StreamReader to read from a file. Dim sr As StreamReader = New StreamReader("TestFile.txt") Dim line As String ' Read and display the lines from the file until the end ' of the file is reached. Do line = sr.ReadLine() Console.WriteLine(Line) Loop Until line Is Nothing sr.Close() Catch E As Exception ' Let the user know what went wrong. Console.WriteLine("The file could not be read:") Console.WriteLine(E.Message) End Try End Sub End Class [C#] using System; using System.IO; class Test { public static void Main() { try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader("TestFile.txt")) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::IO; int main() { try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. StreamReader* sr = new StreamReader(S"TestFile.txt"); try { String* line; // Read and display lines from the file until the end of // the file is reached. while (line = sr->ReadLine()) { Console::WriteLine(line); } } __finally { if (sr) __try_cast<IDisposable*>(sr)->Dispose(); } } catch (Exception* e) { // Let the user know what went wrong. Console::WriteLine(S"The file could not be read:"); Console::WriteLine(e->Message); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
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
StreamReader Members | System.IO Namespace | Encoding | Stream | StreamWriter | 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