blog

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

Stream Reader Object in C#

Sumit Kesarwani3253 18-May-2013

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

Stream Reader Object in C#


Updated 18-Sep-2014

Leave Comment

Comments

Liked By