Reader Level:

StreamReader and StreamWriter Classes in C#

By Praveen Kumar on Dec 11, 2009
In this article I will explain you about StreamReader and StreamWriter Classes in C#.

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.

Listing 6.10: StreamReader and StreamWriter Example


class StreamRW
    public StreamRW()
        //Call the Writer Method

        //Call the Reader Method
    public static void Main()
        StreamRW srw = new StreamRW();
    //Writer Method
    private void Writer()
            //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!");

            //Close the writer and file
        catch (IOException e)
            Console.WriteLine("An IO Exception Occurred :" + e);
    private void Reader()
            //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.WriteLine("Data Read Complete!");

            //Close the file
        catch (IOException e)
            Console.WriteLine("A Error Occurred :" + e);


Output of above Listing 6.10.

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.


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#.

visual C-sharp.jpg The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

Praveen Kumar

C# Corner Chief Editor and 3-times Microsoft MVP. I hold Masters degree in Computer Science and Applications and Bachelor’s degree in Mathematics. I am responsible for content publishing, product development, and m... Read more