By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
StreamReader and StreamWriter are classes in C# used for reading and writing text files. Mastering these classes is crucial for handling file I/O operations efficiently. In real-world applications, these skills are essential for tasks like logging, data processing, and configuration management. Misunderstanding these concepts can lead to data corruption, inefficient code, or security vulnerabilities. For instance, improperly closing a file stream can result in data loss or resource leaks.
csharp FileStream fileStream = new FileStream("example.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
⚠️ Pitfall: Not specifying the correct FileMode can overwrite existing data.
Initialize StreamReader
csharp StreamReader reader = new StreamReader(fileStream);
⚠️ Pitfall: Not checking if the file exists can cause exceptions.
Read Data
csharp string line = reader.ReadLine(); string allText = reader.ReadToEnd();
⚠️ Pitfall: Not handling end-of-file conditions can cause errors.
Initialize StreamWriter
csharp StreamWriter writer = new StreamWriter(fileStream);
⚠️ Pitfall: Not specifying the correct encoding can corrupt data.
Write Data
csharp writer.Write("Hello"); writer.WriteLine("World");
⚠️ Pitfall: Not flushing the stream can result in incomplete writes.
Dispose of Resources
csharp reader.Dispose(); writer.Dispose();
csharp using (StreamReader reader = new StreamReader("example.txt")) { string text = reader.ReadToEnd(); }
Experts view StreamReader and StreamWriter as tools for efficient, reliable file I/O. They focus on resource management, correct encoding, and handling exceptions gracefully. Instead of memorizing methods, they understand the underlying stream mechanics and apply best practices like using statements for automatic resource disposal.
Exam trap: Questions about resource management.
The mistake: Incorrect encoding.
Exam trap: Questions about data integrity.
The mistake: Not handling exceptions.
Exam trap: Questions about error handling.
The mistake: Overwriting files unintentionally.
Scenario: You need to read a configuration file and write logs to a text file.Question: How do you implement this using StreamReader and StreamWriter? Solution: 1. Read Configuration: csharp using (StreamReader reader = new StreamReader("config.txt")) { string config = reader.ReadToEnd(); } 2. Write Logs: csharp using (StreamWriter writer = new StreamWriter("log.txt", true)) { writer.WriteLine("Log entry"); } Answer: The configuration is read, and logs are appended to the log file.Why it works: Proper use of using statements and correct file modes.
csharp using (StreamReader reader = new StreamReader("config.txt")) { string config = reader.ReadToEnd(); }
csharp using (StreamWriter writer = new StreamWriter("log.txt", true)) { writer.WriteLine("Log entry"); }
Scenario: You need to read a file line by line and process each line.Question: How do you implement this using StreamReader? Solution:
using (StreamReader reader = new StreamReader("data.txt")) { string line; while ((line = reader.ReadLine()) != null) { // Process the line } }
Answer: Each line is read and processed.Why it works: Correct use of ReadLine and looping.
using (StreamReader reader = new StreamReader("file.txt"))
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.