Reader Level:
ARTICLE

StreamReader and StreamWriter Classes in C#

Posted by Praveen Kumar Articles | Visual C# December 11, 2009
In this article I will explain you about StreamReader and StreamWriter Classes in C#.
  • 0
  • 0
  • 29700
 

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


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("A Error Occurred :" + e);
        }
        Console.ReadLine();
    }
}


outputListitng6.10.gif

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.

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

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.

COMMENT USING

Trending up