Skip to Content
Course content

201: StreamReader and StreamWriter

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Wait, why bother with these when File.WriteAllText exists?

I get this question a lot. If you just need to dump a small string into a file, File.WriteAllText is great because it's a one-liner. But here is the reality: in professional software, you're often dealing with data that is too large to fit comfortably in memory. If you try to use ReadAllText on a 2GB log file, your application is going to crash with an OutOfMemoryException.

StreamReader and StreamWriter are different because they don't load the whole file at once. They act like a straw, sipping data one piece at a time. This keeps your memory footprint low regardless of whether the file is ten lines or ten million lines long.

How do I add new data to a file without overwriting what's already there?

By default, StreamWriter is aggressive—it wipes the file clean the moment you open it. If you're building something like a combat log for a game or a system audit trail, that's the last thing you want. To fix this, you pass a boolean true as the second argument in the constructor to enable "append mode."


string logPath = "session_log.txt";
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

// The 'true' here tells C# to append to the file instead of overwriting it
using (StreamWriter writer = new StreamWriter(logPath, true))
{
    writer.WriteLine($"[{timestamp}] Player entered the Forbidden Forest.");
    writer.WriteLine($"[{timestamp}] Player picked up a Rusty Sword.");
}

I always use the using statement here. It ensures the file is closed and the buffer is flushed to disk immediately, even if the code throws an error. If you forget it, you might find that your file stays empty because the data is still sitting in a memory buffer waiting to be written.

What's the cleanest way to read a file line-by-line?

You don't know exactly how many lines are in a file until you've actually reached the end. The standard pattern I use is a while loop that keeps calling ReadLine() until it returns null. null is C#'s way of telling you, "That's it, you've hit the bottom of the file."

Let's say we want to read that log file we just created and search for a specific keyword:


string logPath = "session_log.txt";
string searchKeyword = "Rusty Sword";

using (StreamReader reader = new StreamReader(logPath))
{
    string line;
    // Read one line at a time until the end of the stream
    while ((line = reader.ReadLine()) != null)
    {
        if (line.Contains(searchKeyword))
        {
            Console.WriteLine($"Found it: {line}");
        }
    }
}

Notice how we declare string line outside the loop. We do this so the variable is available for the if check inside the loop body. It's a tight, efficient way to process huge amounts of text without stressing the CPU or RAM.




📋 Practical Task

Exercise: Player Inventory Save/Load System

You are building a simple RPG. You need to create a system that saves a player's inventory to a text file and then loads it back into the game when the program restarts.

  • Create a List<string> containing at least five items (e.g., "Health Potion", "Iron Shield", "Magic Map").
  • Use a StreamWriter to save each item from the list into a file named inventory.txt, ensuring each item is on its own line.
  • Clear your list (or create a new empty one).
  • Use a StreamReader to read the inventory.txt file line-by-line and add each item back into your list.
  • Finally, print the recovered list to the console to verify that the load was successful.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.