StreamReader and StreamWriter Classes in C#

Introduction

This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

The StreamReader and StreamWriter classes extend the TextReader and TextWriter classes to provide the most widely used stream for writing textual data. In addition, these classes are encoding sensitive and can be used to read and write text in different encoded formats. By default, the encoding used is UTF8. Since these classes override the TextReader and TextWriter classes, all the underlying methods are the same as those listed in Tables 6.11 and 6.12. The StreamRW class shown in Listing 6.10 uses StreamReader and StreamWriter to read and write a profile from a text file.

What is StreamReader and StreamWriter in C#?

StreamReader is used for reading character data from a stream, such as a file stream, it is often used to read text files, but it can also be used to read from other sources like network streams or memory streams.

StreamWriter is used for writing character data to a stream, such as a file stream, it is commonly used for creating or appending text files, but it can also be used to write to other destinations like network streams or memory streams.

Example

using System;
using System.IO;

public class StreamRW
{
    // Constructor
    public StreamRW()
    {
        // Call the Writer Method
        Writer();

        // Call the Reader Method
        Reader();
    }

    public static void Main()
    {
        StreamRW srw = new StreamRW();
    }

    // Writer Method
    private void Writer()
    {
        try
        {
            // Open or create a new file called "urprofile.txt"
            FileInfo f1 = new FileInfo("urprofile.txt");

            // Get a StreamWriter for the file
            StreamWriter sw = f1.CreateText();

            Console.WriteLine("Welcome to the profile program");
            Console.Write("Name: ");

            // Get the name from the console
            string name = Console.ReadLine();

            // Write to file
            sw.WriteLine("Name: " + name);

            Console.Write("Country: ");
            string country = Console.ReadLine();

            // Write to file
            sw.WriteLine("Country: " + country);

            Console.Write("Age: ");
            string age = Console.ReadLine();

            // Write to file
            sw.WriteLine("Age: " + age);

            Console.WriteLine("Thank You");
            Console.WriteLine("Information Saved!");
            Console.WriteLine();

            // Close the writer and file
            sw.Close();
        }
        catch (IOException e)
        {
            Console.WriteLine("An IO Exception Occurred: " + e);
        }
    }

    private void Reader()
    {
        try
        {
            // Open the file
            FileInfo f2 = new FileInfo("urprofile.txt");

            // Get the StreamReader
            StreamReader sr = f2.OpenText();

            Console.WriteLine("Reading profile from file");

            // Peek to see if the next character exists
            while (sr.Peek() > -1)
            {
                // Read a line from the file and display it on the console
                Console.WriteLine(sr.ReadLine());
            }

            Console.WriteLine("Data Read Complete!");

            // Close the file
            sr.Close();
        }
        catch (IOException e)
        {
            Console.WriteLine("An Error Occurred: " + e);
        }

        Console.ReadLine();
    }
}

Output

In this example, two methods-Writer and Reader-are called from the constructor. The Writer method opens file urprofile.txt and gets a StreamWriter for the file. Input from the console screen is saved to the file and the file is closed. The Reader method opens up the file again and gets a StreamReader. Data is then read from the file line by line and displayed on the console. Finally, the file is closed.

outputListitng6.10.gif

Conclusion

Hope this article would have helped you in understanding StreamReader and StreamWriter Classes in C#. See other articles on the website on .NET and C#.


Similar Articles
C# Corner
C# Corner started as an online community for software developers in 1999.