How do you read and write files in C#?

πŸ’‘ Concept: File Reading and Writing

File handling in C# involves reading data from files and writing data to files using various classes and methods.

πŸ“˜ Quick Intro

C# provides multiple APIs like StreamReader, StreamWriter, and File class to facilitate file I/O operations.

🧠 Analogy

Think of file handling like using a pen and paper β€” you can read what’s written or write new content.

πŸ”§ Technical Explanation

  • πŸ“„ StreamReader reads text from a file sequentially.
  • ✍️ StreamWriter writes text to a file, creating or overwriting it.
  • πŸ“ The File class provides static methods for quick file operations.
  • βš™οΈ Supports synchronous and asynchronous methods for better performance.
  • πŸ› οΈ Proper disposal of streams is important to release resources.

🎯 Use Cases

  • βœ… Reading configuration files.
  • βœ… Logging application data.
  • βœ… Processing large text files.
  • βœ… Saving user data or reports.

πŸ’» Code Example


using System;
using System.IO;

class Program {
    static void Main() {
        // Writing to a file
        using (StreamWriter writer = new StreamWriter(""example.txt"")) {
            writer.WriteLine(""Hello, World!"");
        }

        // Reading from a file
        using (StreamReader reader = new StreamReader(""example.txt"")) {
            string content = reader.ReadToEnd();
            Console.WriteLine(content);
        }
    }
}

❓ Interview Q&A

Q1: What class is used to read text files?
A: StreamReader.

Q2: How to write text to a file?
A: Using StreamWriter or File.WriteAllText.

Q3: How to ensure files are properly closed?
A: Use 'using' statement or call Dispose.

Q4: Can file operations be asynchronous?
A: Yes, using async methods.

Q5: What namespace contains file handling classes?
A: System.IO.

Q6: What happens if a file doesn't exist?
A: Reading throws exception; writing creates new file.

Q7: How to append text to an existing file?
A: Use StreamWriter with append parameter.

Q8: What is the difference between StreamReader and File.ReadAllText?
A: StreamReader reads streams; File.ReadAllText reads all content at once.

Q9: Can you read binary files with these classes?
A: No, use BinaryReader for binary data.

Q10: How to handle large files efficiently?
A: Read line by line or use buffering.

πŸ“ MCQs

Q1. What class reads text files?

  • StreamWriter
  • File
  • StreamReader
  • BinaryReader

Q2. How to write text to file?

  • StreamReader
  • StreamWriter or File.WriteAllText
  • File.ReadAllText
  • Console.Write

Q3. How to ensure files close?

  • 'using' statement
  • Close()
  • Dispose()
  • Flush()

Q4. Can file ops be async?

  • No
  • Yes
  • Sometimes
  • Never

Q5. Namespace for file handling?

  • System.Net
  • System.IO
  • System.Text
  • System.Threading

Q6. What if file missing?

  • Creates file
  • Exception when reading
  • Deletes file
  • Ignores

Q7. How to append text?

  • StreamReader append
  • StreamWriter append
  • File.ReadAllText append
  • Console.Write

Q8. StreamReader vs File.ReadAllText?

  • Same
  • StreamReader reads streams
  • File.ReadAllText reads streams
  • None

Q9. Read binary files?

  • Yes
  • No
  • Sometimes
  • Always

Q10. Handle large files?

  • Read all at once
  • Read line by line
  • Ignore
  • Skip

πŸ’‘ Bonus Insight

Efficient file I/O is crucial for performance, especially with large datasets in C# applications.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: