Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Read in a file in C# with StreamReader
The StreamReader class in C# is used to read text files efficiently. It provides methods to read characters, lines, or the entire content of a text file. StreamReader is part of the System.IO namespace and implements IDisposable, making it suitable for use with using statements for automatic resource cleanup.
Syntax
Following is the syntax to create a StreamReader object −
StreamReader sr = new StreamReader("filename.txt");
Following is the syntax to use StreamReader with automatic disposal −
using (StreamReader sr = new StreamReader("filename.txt")) {
// read operations
}
Common StreamReader Methods
| Method | Description |
|---|---|
ReadLine() |
Reads a single line from the file |
ReadToEnd() |
Reads the entire file content as a string |
Read() |
Reads a single character from the file |
EndOfStream |
Property that indicates if the end of file is reached |
Using ReadLine() to Read First Line
The ReadLine() method reads a single line from the text file −
using System.IO;
using System;
public class Program {
public static void Main() {
string str;
using (StreamWriter sw = new StreamWriter("hello.txt")) {
sw.WriteLine("Hello");
sw.WriteLine("World");
sw.WriteLine("C# Programming");
}
using (StreamReader sr = new StreamReader("hello.txt")) {
str = sr.ReadLine();
}
Console.WriteLine("First line: " + str);
}
}
The output of the above code is −
First line: Hello
Using ReadToEnd() to Read Entire File
The ReadToEnd() method reads all content from the current position to the end of the file −
using System.IO;
using System;
public class Program {
public static void Main() {
using (StreamWriter sw = new StreamWriter("sample.txt")) {
sw.WriteLine("Line 1: Welcome to C#");
sw.WriteLine("Line 2: StreamReader Tutorial");
sw.WriteLine("Line 3: File Reading Example");
}
using (StreamReader sr = new StreamReader("sample.txt")) {
string content = sr.ReadToEnd();
Console.WriteLine("Complete file content:");
Console.WriteLine(content);
}
}
}
The output of the above code is −
Complete file content: Line 1: Welcome to C# Line 2: StreamReader Tutorial Line 3: File Reading Example
Reading All Lines with Loop
You can read all lines one by one using a loop with the EndOfStream property −
using System.IO;
using System;
public class Program {
public static void Main() {
using (StreamWriter sw = new StreamWriter("numbers.txt")) {
sw.WriteLine("One");
sw.WriteLine("Two");
sw.WriteLine("Three");
sw.WriteLine("Four");
}
using (StreamReader sr = new StreamReader("numbers.txt")) {
int lineNumber = 1;
while (!sr.EndOfStream) {
string line = sr.ReadLine();
Console.WriteLine($"Line {lineNumber}: {line}");
lineNumber++;
}
}
}
}
The output of the above code is −
Line 1: One Line 2: Two Line 3: Three Line 4: Four
Conclusion
StreamReader in C# provides efficient methods for reading text files, including ReadLine() for single lines, ReadToEnd() for entire content, and loop-based reading with EndOfStream. Always use it within a using statement to ensure proper resource disposal and avoid memory leaks.
