StreamReader Class
Represents a TextReader that reads from a byte input stream in a particular encoding.
System.IO
Assembly:
System.Runtime.dll
Syntax
public class StreamReader : TextReader
Remarks
The StreamReader
class provides a convenient way to read characters from a stream. It handles character encoding and buffering to improve reading performance. You can use StreamReader
to read text files, network streams, or any other stream that contains text data.
When you create an instance of StreamReader
, you can specify the encoding to use. If no encoding is specified, the constructor attempts to detect the encoding of the stream by looking for a byte order mark (BOM). If no BOM is found, the stream's detected encoding is used.
It is important to dispose of StreamReader
instances when you are finished with them to release any unmanaged resources they may be holding, such as file handles. This can be done using a using
statement in C# or by explicitly calling the Dispose()
method.
Constructors
StreamReader(Stream stream)
Initializes a new instance of the StreamReader
class for the specified stream and default encoding (UTF-8 without BOM).
public StreamReader(Stream stream);
StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks)
Initializes a new instance of the StreamReader
class for the specified stream and optionally detects encoding from a byte order mark.
public StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks);
StreamReader(string path)
Initializes a new instance of the StreamReader
class for the specified file path and default encoding (UTF-8 without BOM).
public StreamReader(string path);
StreamReader(string path, System.Text.Encoding encoding)
Initializes a new instance of the StreamReader
class for the specified file path and character encoding.
public StreamReader(string path, System.Text.Encoding encoding);
Methods
Close()
Closes the StreamReader
and the underlying stream.
public override void Close();
Dispose()
Releases the unmanaged resources used by the StreamReader
and optionally releases the managed resources.
public void Dispose();
Peek()
Returns the next character without consuming it from the input stream.
public override int Peek();
Read()
Reads the next character from the input stream and advances the character position by one.
public override int Read();
Read(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current stream and writes the data to a buffer.
public override int Read(char[] buffer, int index, int count);
ReadLine()
Reads a line of characters from the current stream and returns the data as a string.
public override string ReadLine();
ReadToEnd()
Reads all characters from the current position to the end of the stream and returns them as one string.
public override string ReadToEnd();
Properties
BaseStream
Gets the underlying stream.
protected Stream BaseStream { get; }
CurrentEncoding
Gets the character encoding for the current StreamReader
object.
public System.Text.Encoding CurrentEncoding { get; }
EndOfStream
Gets a value indicating whether the end of the stream has been reached.
public bool EndOfStream { get; }
Example
The following C# code example demonstrates how to read a text file line by line using StreamReader
.
using System;
using System.IO;
using System.Text;
public class Example
{
public static void Main(string[] args)
{
string filePath = "myFile.txt"; // Replace with your file path
// Create a sample file for demonstration
File.WriteAllText(filePath, "This is the first line.\nThis is the second line.\nAnd finally, the third line.", Encoding.UTF8);
try
{
using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8))
{
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 (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 sample file
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
}