StreamReader and StreamWriter help you read from and write to files easily. They make working with text files simple and organized.
StreamReader and StreamWriter in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
using System.IO; // To read from a file using (StreamReader reader = new StreamReader("filename.txt")) { string line = reader.ReadLine(); } // To write to a file using (StreamWriter writer = new StreamWriter("filename.txt")) { writer.WriteLine("text to write"); }
Always use using to automatically close the file after reading or writing.
StreamReader reads text line by line or all at once, StreamWriter writes text line by line or all at once.
using (StreamReader reader = new StreamReader("data.txt")) { string content = reader.ReadToEnd(); Console.WriteLine(content); }
using (StreamWriter writer = new StreamWriter("log.txt")) { writer.WriteLine("Log started"); writer.WriteLine("Another log entry"); }
using (StreamReader reader = new StreamReader("notes.txt")) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } }
This program writes two lines to a file named "example.txt" and then reads the file line by line, printing each line to the console.
using System; using System.IO; class Program { static void Main() { string fileName = "example.txt"; // Write some lines to the file using (StreamWriter writer = new StreamWriter(fileName)) { writer.WriteLine("Hello, world!"); writer.WriteLine("Welcome to StreamWriter."); } // Read and print the file content using (StreamReader reader = new StreamReader(fileName)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } } }
If the file does not exist, StreamWriter will create it automatically.
StreamReader throws an error if the file does not exist, so make sure the file is there before reading.
Always close or dispose StreamReader and StreamWriter to free system resources; using using does this automatically.
StreamReader reads text from files easily, line by line or all at once.
StreamWriter writes text to files, creating or overwriting them.
Use using blocks to handle files safely and cleanly.
Practice
StreamReader class in C#?Solution
Step 1: Understand StreamReader's role
StreamReaderis designed to read text data from files.Step 2: Differentiate from StreamWriter
StreamWriterwrites text, not reads it.Final Answer:
To read text from a file -> Option AQuick Check:
StreamReader reads files = D [OK]
- Confusing StreamReader with StreamWriter
- Thinking StreamReader creates files
- Assuming StreamReader deletes files
StreamWriter in C#?Solution
Step 1: Recognize correct StreamWriter instantiation
The correct way is to usenew StreamWriter("file.txt")inside ausingblock for safe disposal.Step 2: Check syntax correctness
using (StreamWriter writer = new StreamWriter("file.txt")) { } usesusingwith proper syntax and constructor call.Final Answer:
using (StreamWriter writer = new StreamWriter("file.txt")) { } -> Option BQuick Check:
Correct StreamWriter syntax = B [OK]
- Missing 'new' keyword
- Not using 'using' block for disposal
- Incorrect method calls like .read() on StreamWriter
using (var writer = new StreamWriter("test.txt")) {
writer.WriteLine("Hello");
writer.WriteLine("World");
}
using (var reader = new StreamReader("test.txt")) {
string content = reader.ReadToEnd();
Console.Write(content);
}Solution
Step 1: Understand StreamWriter.WriteLine behavior
Each WriteLine writes the string plus a newline character at the end.Step 2: ReadToEnd reads full content including newlines
The reader reads the entire file content, preserving newlines.Final Answer:
Hello\nWorld\n -> Option CQuick Check:
WriteLine adds newline, ReadToEnd reads all [OK]
- Ignoring newline characters added by WriteLine
- Assuming WriteLine writes without newlines
- Confusing output formatting in Console.Write
StreamReader reader = new StreamReader("data.txt");
string line = reader.ReadLine();
Console.WriteLine(line);
reader.Close();Solution
Step 1: Check resource management
The code opens a StreamReader but does not use ausingblock, risking resource leaks if exceptions occur.Step 2: Confirm method correctness
ReadLine()is correct to read one line;Close()is called but manual closing is less safe thanusing.Final Answer:
Missing 'using' block to ensure file closure -> Option AQuick Check:
Use 'using' to auto-close files [OK]
- Not using 'using' block for automatic disposal
- Confusing ReadLine with ReadAll
- Calling Close before reading
StreamReader and StreamWriter. Which code snippet correctly performs this task?Solution
Step 1: Check proper resource management
using (var reader = new StreamReader("source.txt")) { using (var writer = new StreamWriter("dest.txt")) { string line; while ((line = reader.ReadLine()) != null) { writer.WriteLine(line); } } } uses nestedusingblocks to ensure both reader and writer are properly closed.Step 2: Verify reading and writing logic
It reads line by line until null, writing each line to the destination file correctly.Final Answer:
Correct nested using blocks with line-by-line copy -> Option DQuick Check:
Nested using + line loop = A [OK]
- Not disposing writer properly
- Reversing reader and writer order in using blocks
- Not looping to read all lines
- Not disposing writer in option D
