Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide

In C# file I/O, streams serve as an intermediary between your application and the underlying file system. They allow you to read from or write to files in a memory‑efficient way, especially when dealing with large data sets.

Instead of loading an entire 100 MB file into memory—which can cause your app to freeze—streams break the data into manageable chunks. This approach keeps performance smooth and prevents crashes.

Below you’ll find concise examples that demonstrate how to read and write files using StreamReader and StreamWriter. Follow the step‑by‑step instructions to integrate these patterns into your own projects.

StreamReader – Reading Files

The StreamReader class reads text from a file one line at a time, passing the data through a buffer before your application processes it.

Assume you have a file named Example.txt on the D drive containing two lines:

Create a console application and add the following code to Program.cs:

Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide

using System;
using System.IO;

namespace DemoApplication
{
    class Tutorial
    {
        static void Main(string[] args)
        {
            string path = @"D:\Example.txt";

            using (StreamReader sr = File.OpenText(path))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }

            Console.ReadKey();
        }
    }
}

Explanation:

  1. Instantiate StreamReader with File.OpenText, opening the file in read‑only mode.
  2. Read each line with ReadLine(), which transfers data from the file to the buffer and then to the line variable.
  3. Output each line to the console.

Running this program in Visual Studio displays:

Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide

The two lines from Example.txt appear correctly, proving that the reader works as intended.

StreamWriter – Writing to Files

Conversely, StreamWriter writes text from your application to a file via a buffer. Below is a simple example that appends a line to the same Example.txt file.

Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide

using System;
using System.IO;

namespace DemoApplication
{
    class Tutorial
    {
        static void Main(string[] args)
        {
            string path = @"D:\Example.txt";

            using (StreamWriter sw = File.AppendText(path))
            {
                sw.WriteLine("Guru99 – ASP.Net");
            }

            Console.WriteLine(File.ReadAllText(path));
            Console.ReadKey();
        }
    }
}

Explanation:

  1. Instantiate StreamWriter with File.AppendText, opening the file in append mode.
  2. Write a new line using WriteLine(), which pushes the text into the buffer and then to the file.
  3. Close the writer automatically via the using block.
  4. Read the entire file content with File.ReadAllText to confirm the write operation.

Executing this program shows:

Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide

The console displays all three lines, confirming that the new entry was appended successfully.

Key Takeaways

C Language

  1. Mastering C++ Streams: cout, cin, cerr, clog – Practical Guide
  2. C++ Structs Explained with a Practical Example
  3. Mastering std::list in C++: Syntax, Functions & Practical Examples
  4. C# Abstract Classes: A Practical Tutorial with Code Examples
  5. C# Windows Forms Development: A Hands‑On Tutorial
  6. C# Serialization & Deserialization: A Practical Example
  7. Reading Files in Java with BufferedReader – A Practical Guide with Examples
  8. Master Java Reflection API: A Practical Guide with Code Examples
  9. Creating ZIP Archives in Python: From Full Directory to Custom File Selection
  10. Python Calendar Module: Expert Guide with Code Examples