Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide
In C# file I/O, streams serve as an intermediary between your application and the underlying file system. They allow you to read from or write to files in a memory‑efficient way, especially when dealing with large data sets.
Instead of loading an entire 100 MB file into memory—which can cause your app to freeze—streams break the data into manageable chunks. This approach keeps performance smooth and prevents crashes.
Below you’ll find concise examples that demonstrate how to read and write files using StreamReader and StreamWriter. Follow the step‑by‑step instructions to integrate these patterns into your own projects.
StreamReader – Reading Files
The StreamReader class reads text from a file one line at a time, passing the data through a buffer before your application processes it.
Assume you have a file named Example.txt on the D drive containing two lines:
- Guru99 – .Net
- Guru99 – C#
Create a console application and add the following code to Program.cs:

using System;
using System.IO;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
string path = @"D:\Example.txt";
using (StreamReader sr = File.OpenText(path))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
Console.ReadKey();
}
}
}
Explanation:
- Instantiate
StreamReaderwithFile.OpenText, opening the file in read‑only mode. - Read each line with
ReadLine(), which transfers data from the file to the buffer and then to thelinevariable. - Output each line to the console.
Running this program in Visual Studio displays:

The two lines from Example.txt appear correctly, proving that the reader works as intended.
StreamWriter – Writing to Files
Conversely, StreamWriter writes text from your application to a file via a buffer. Below is a simple example that appends a line to the same Example.txt file.

using System;
using System.IO;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
string path = @"D:\Example.txt";
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine("Guru99 – ASP.Net");
}
Console.WriteLine(File.ReadAllText(path));
Console.ReadKey();
}
}
}
Explanation:
- Instantiate
StreamWriterwithFile.AppendText, opening the file in append mode. - Write a new line using
WriteLine(), which pushes the text into the buffer and then to the file. - Close the writer automatically via the
usingblock. - Read the entire file content with
File.ReadAllTextto confirm the write operation.
Executing this program shows:

The console displays all three lines, confirming that the new entry was appended successfully.
Key Takeaways
- Streams decouple file I/O from the application logic, enabling efficient handling of large files.
- Use
StreamReaderto read text line‑by‑line. - Use
StreamWriterto write or append text, ensuring you close the stream to release file handles.
C Language
- Mastering C++ Streams: cout, cin, cerr, clog – Practical Guide
- C++ Structs Explained with a Practical Example
- Mastering std::list in C++: Syntax, Functions & Practical Examples
- C# Abstract Classes: A Practical Tutorial with Code Examples
- C# Windows Forms Development: A Hands‑On Tutorial
- C# Serialization & Deserialization: A Practical Example
- Reading Files in Java with BufferedReader – A Practical Guide with Examples
- Master Java Reflection API: A Practical Guide with Code Examples
- Creating ZIP Archives in Python: From Full Directory to Custom File Selection
- Python Calendar Module: Expert Guide with Code Examples