In C#, working with files is a common task whether for reading logs, configuration data or saving program outputs. The StreamReader and StreamWriter classes provide efficient ways to read and write characters from and to a stream. Both classes are part of the System.IO namespace and are primarily used for text-based file operations.
StreamReader
StreamReader is a class used to read characters from a stream in a specific encoding. It is commonly used to read text files line by line or in full.
Syntax
StreamReader sr = new StreamReader(string path);
Constructors
- StreamReader(string path): Initializes a new instance for the specified file path.
- StreamReader(Stream stream): Initializes with an existing stream.
- StreamReader(string path, Encoding encoding): Uses a specified encoding for the file.
- StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks): Initializes with encoding and an option to detect byte order marks.
Methods
| Method | Description |
|---|---|
| Read() | Reads the next character from the input stream and advances the character position by one. |
| ReadLine() | Reads a line of characters from the current stream and returns it as a string. |
| ReadToEnd() | Reads all characters from the current position to the end of the stream. |
| Peek() | Returns the next available character without advancing the reader’s position. |
| Close() | Closes the StreamReader object and the underlying stream. |
| Dispose() | Releases all resources used by the StreamReader. |
Example: Reading a File
using System;
using System.IO;
class Program
{
static void Main()
{
// Open sample.txt for reading using StreamReader
using (StreamReader sr = new StreamReader("sample.txt"))
{
string line;
// Read file line by line until end of file
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
This program opens sample.txt and reads it line by line using ReadLine(). Each line is printed on the console. The using statement ensures the file is closed after reading.
StreamWriter
StreamWriter is a class used to write characters to a stream in a specific encoding. It is commonly used to create or append text files.
Syntax
StreamWriter sw = new StreamWriter(string path);
Constructors
- StreamWriter(string path): Creates or overwrites the file at the specified path.
- StreamWriter(string path, bool append): Appends text to an existing file if append is true, otherwise overwrites it.
- StreamWriter(Stream stream): Writes to an existing stream.
- StreamWriter(string path, bool append, Encoding encoding): Uses specified encoding while writing.
Methods
| Method | Description |
|---|---|
| Write(string value) | Writes a string to the stream without a newline. |
| WriteLine(string value) | Writes a string followed by a newline. |
| Flush() | Clears all buffers for the current writer and causes any buffered data to be written to the file. |
| Close() | Closes the StreamWriter and the underlying stream. |
| Dispose() | Releases all resources used by the StreamWriter. |
Example: Writing to a File
using System;
using System.IO;
class Program
{
static void Main()
{
// Open sample.txt for writing, append set to true
using (StreamWriter sw = new StreamWriter("sample.txt", true))
{
sw.WriteLine("Hello, StreamWriter!");
sw.WriteLine("Appending new content to file.");
}
}
}
This program writes text into sample.txt. The second constructor parameter true ensures new content is appended instead of overwriting the file. The WriteLine() method writes text followed by a newline character.