Chapter 21: File I/O and Serialization

Until now, all our data lived only in memory – as soon as the program stopped, poof! Everything was gone. Now we’re going to learn how to save data to files and read it back, and how to save complex objects (like lists of students, settings, game progress…) as JSON or XML – the two most popular formats in the world.

I’m going to explain everything very slowly, step by step, with tons of real-life examples, clear analogies, and practical mini-projects — just like we’re sitting together in Hyderabad looking at the same screen. Let’s dive in! 🚀

1. Reading and Writing Files – The Basics

C# gives us several ways to work with files. We’ll start with the easiest and most common ones.

A. The Simplest Way: File Class (High-Level Helpers)

C#

Important: File.WriteAllText() and WriteAllLines() overwrite the file if it already exists. Use AppendAllText() or AppendAllLines() to add without losing previous content.

B. Reading/Writing Line by Line – StreamReader & StreamWriter (More Control)

When you need to read or write huge files or process line by line (recommended for big files).

C#

Why use using? It automatically closes the file when you’re done – very important to avoid file locks!

2. Checking If File Exists, Deleting, Copying, Moving

C#

3. Serialization – Saving Objects to Files (JSON & XML)

Serialization = turning an object (or list of objects) into a string or byte stream that you can save to a file or send over the internet. Deserialization = turning that string back into an object.

A. JSON Serialization – The Modern Standard (System.Text.Json – Built-in since .NET Core 3)

Recommended in 2026: System.Text.Json (fast, secure, built-in)

C#

Output JSON:

JSON

B. Newtonsoft.Json (Json.NET) – Still Very Popular (Especially in Older Projects)

Add NuGet package: Newtonsoft.Json

C#

Both libraries are great, but in new projects (2026) → prefer System.Text.Json (faster, built-in, more secure by default).

4. Mini-Project: Todo List App with File Saving

C#

Output after running a few times:

text

Summary – What We Learned Today

  • File I/O → File.WriteAllText(), File.ReadAllText(), StreamWriter, StreamReader
  • Check/Delete/Copy/Move → File.Exists(), File.Delete(), File.Copy(), File.Move()
  • JSON Serialization → System.Text.Json (built-in) or Newtonsoft.Json (popular)
  • Save & Load entire objects/lists → super easy with JsonSerializer.Serialize() / Deserialize()
  • Always use using blocks for file streams → auto-closes files

Your Homework (Super Practical!)

  1. Create a new console project called FileAndJsonMaster
  2. Make a Personal Diary App that:
    • Lets user add diary entries (date + text)
    • Saves all entries to diary.json
    • Loads them when program starts
    • Shows all entries sorted by date
    • Lets user search entries by keyword
  3. Bonus: Add ability to delete an entry by number and save again

Next lesson: Asynchronous Programming (async/await) – we’re going to learn how to make your programs do many things at once without freezing!

You’re doing absolutely fantastic! 🎉 Any part confusing? Want more examples with XML serialization or binary files? Just tell me — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *