StreamReader and StreamWriter in C#
What are StreamReader and StreamWriter in C#?
**StreamReader** and **StreamWriter** are classes in the System.IO
namespace used for reading and writing text files in C#. These classes provide an efficient way to handle text-based file operations.
Key Features of StreamReader & StreamWriter
- StreamReader reads text files **line by line** or fully at once.
- StreamWriter writes text data to a file **efficiently**.
- Supports **buffering**, making read and write operations faster.
- Works well for handling **large text files**.
Writing to a File Using StreamWriter
The StreamWriter
class allows writing text data to a file efficiently.
Example: Writing to a File Using StreamWriter
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("Hello, StreamWriter in C#!");
writer.WriteLine("This is a test file.");
}
Console.WriteLine("Text written to file successfully.");
}
}
// Output:
// Text written to file successfully.
The **using
** statement ensures the file is properly closed after writing.
Appending Text Using StreamWriter
You can **append** text to an existing file by opening it in **append mode**.
Example: Appending Text to a File
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Appending this line to the file.");
}
Console.WriteLine("Text appended to file successfully.");
}
}
// Output:
// Text appended to file successfully.
The **true
** parameter ensures the file is opened in **append mode** instead of overwriting its content.
Reading from a File Using StreamReader
The StreamReader
class is used to **read text from a file line by line** or **fully**.
Example: Reading a File Using StreamReader
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
using (StreamReader reader = new StreamReader(filePath))
{
string content = reader.ReadToEnd();
Console.WriteLine("File Content:");
Console.WriteLine(content);
}
}
}
// Output (depends on file content):
// File Content:
// Hello, StreamWriter in C#!
// This is a test file.
// Appending this line to the file.
The **ReadToEnd()
** method reads the entire content of the file at once.
Reading a File Line by Line
You can read a file **line by line** using ReadLine()
.
Example: Reading a File Line by Line
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
using (StreamReader reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
// Output (depends on file content):
// Hello, StreamWriter in C#!
// This is a test file.
// Appending this line to the file.
The **ReadLine()
** method reads the file one line at a time, reducing memory usage for large files.
Best Practices for Using StreamReader & StreamWriter
- Use **
using
statements** to ensure files are properly closed. - Prefer **
ReadLine()
** for large files instead of **ReadToEnd()
**. - Check **file existence** using
File.Exists()
before reading. - Use **buffering** for efficient file operations.