Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Reading and Writing Text Files with StreamReader and StreamWriter
📖 Scenario: You are working on a simple note-taking app. You want to save notes to a file and read them back later.
🎯 Goal: Learn how to use StreamWriter to write text to a file and StreamReader to read text from a file in C#.
📋 What You'll Learn
Create a text file with some notes using StreamWriter
Set the file path in a variable
Read the notes back from the file using StreamReader
Print the notes to the console
💡 Why This Matters
🌍 Real World
Reading and writing text files is common in apps that save user data, logs, or configuration settings.
💼 Career
Many programming jobs require working with files to store and retrieve data, making StreamReader and StreamWriter essential tools.
Progress0 / 4 steps
1
Create a file path variable
Create a string variable called filePath and set it to the exact value "notes.txt".
C Sharp (C#)
Hint
Use string filePath = "notes.txt"; to set the file path.
2
Write notes to the file using StreamWriter
Use StreamWriter with the variable filePath to write these exact lines to the file: "Shopping list:" "- Milk" "- Bread" Use a using block to open and close the writer.
C Sharp (C#)
Hint
Use using (var writer = new System.IO.StreamWriter(filePath)) and writer.WriteLine() for each line.
3
Read notes from the file using StreamReader
Use StreamReader with the variable filePath inside a using block to read all lines from the file. Store the result in a string variable called notes using ReadToEnd().
C Sharp (C#)
Hint
Use using (var reader = new System.IO.StreamReader(filePath)) and notes = reader.ReadToEnd();.
4
Print the notes to the console
Write a Console.WriteLine statement to print the variable notes.
C Sharp (C#)
Hint
Use Console.WriteLine(notes); to display the notes.
Practice
(1/5)
1. What is the primary purpose of the StreamReader class in C#?
easy
A. To read text from a file
B. To write text to a file
C. To create a new file
D. To delete a file
Solution
Step 1: Understand StreamReader's role
StreamReader is designed to read text data from files.
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 C
Quick Check:
WriteLine adds newline, ReadToEnd reads all [OK]
Hint: WriteLine adds newline; ReadToEnd reads full text [OK]
Common Mistakes:
Ignoring newline characters added by WriteLine
Assuming WriteLine writes without newlines
Confusing output formatting in Console.Write
4. Identify the error in the following code snippet:
StreamReader reader = new StreamReader("data.txt");
string line = reader.ReadLine();
Console.WriteLine(line);
reader.Close();
medium
A. Missing 'using' block to ensure file closure
B. ReadLine() should be ReadAll()
C. StreamReader cannot read text files
D. reader.Close() should be called before ReadLine()
Solution
Step 1: Check resource management
The code opens a StreamReader but does not use a using block, 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 than using.
Final Answer:
Missing 'using' block to ensure file closure -> Option A
Quick Check:
Use 'using' to auto-close files [OK]
Hint: Always use 'using' to auto-close streams [OK]
Common Mistakes:
Not using 'using' block for automatic disposal
Confusing ReadLine with ReadAll
Calling Close before reading
5. You want to copy the contents of one text file to another using StreamReader and StreamWriter. Which code snippet correctly performs this task?
hard
A. using (var reader = new StreamReader("source.txt")) {
string content = reader.ReadToEnd();
var writer = new StreamWriter("dest.txt");
writer.Write(content);
}
B. using (var writer = new StreamWriter("dest.txt")) {
using (var reader = new StreamReader("source.txt")) {
string line;
while ((line = reader.ReadLine()) != null) {
writer.WriteLine(line);
}
}
}
C. var reader = new StreamReader("source.txt");
var writer = new StreamWriter("dest.txt");
string line = reader.ReadLine();
while (line != null) {
writer.WriteLine(line);
line = reader.ReadLine();
}
reader.Close();
writer.Close();
D. using (var reader = new StreamReader("source.txt")) {
using (var writer = new StreamWriter("dest.txt")) {
string line;
while ((line = reader.ReadLine()) != null) {
writer.WriteLine(line);
}
}
}
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 nested using blocks 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 D
Quick Check:
Nested using + line loop = A [OK]
Hint: Use nested 'using' blocks and loop ReadLine [OK]