Home > DeveloperSection > Blogs > Stream Reader Object in C#

Stream Reader Object in C#


C# C# 
Ratings:
0 Comment(s)
 1723  View(s)
Rate this:

Stream Reader Object in C#

In this blog, I’m trying to explain the concept of stream reader object and how to implement it .

StreamReader reads text files. It is found in the System.IO namespace. We have to wrap the FileStream object into StreamReader and use their methods to manipulate files, but limitation with this is that random access of file is not supported.

Example

using System;

using System.IO;

namespace StreamReaderConsoleApplication

{

    class streamReaderExample

    {

        static void Main(string[] args)

        {

            string str;

            try

            {

                FileStream fs = new FileStream(@"D:\SumitKesarwani\sumit\2013-5-11\StreamReaderConsoleApplication\data.txt", FileMode.Open);

                StreamReader sr = new StreamReader(fs);

                str = sr.ReadLine();

                while (str != null)

                {

                    Console.WriteLine(str);

                    str = sr.ReadLine();

                }

                sr.Close();

            }

            catch(IOException e)

            {

                Console.WriteLine("Exception occured" + e.ToString());

            }

            Console.ReadLine();

        }

    }

}

 Output


Don't want to miss updates? Please click the below button!

Follow MindStick