Q:

Explain StreamReader in C# with Example

0

Explain StreamReader in C# with Example

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

1) C# program to read single line from file

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //create an object of the FileStream in which pass the path  of the  file from which you need read the content.
            FileStream F = new FileStream("abc.txt", FileMode.OpenOrCreate);
            //create the object of the StreamReader class and pass object of FileStream as parameter.
            StreamReader S = new StreamReader(F);
            //Read the content from the file
            String Line = S.ReadLine();
            //Print the content on the screen
            Console.WriteLine(Line);
            //Close the respective files
            S.Close();
            F.Close();
        }
    }
}

Output

This is line 1 written in file.

2) C# program to read all lines from file

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //create an object of the FileStream in which pass the path  of the  file from which you need read the content.
            FileStream F = new FileStream("abc.txt", FileMode.OpenOrCreate);
            //create the object of the StreamReader class and pass object of FileStream as parameter.
            StreamReader S = new StreamReader(F);
            //code to read multiple lines
            String Line = " ";
            while ((Line = S.ReadLine()) != null)
            {
                Console.WriteLine(Line);
            }
            //Close the respective files
            S.Close();
            F.Close();
        }
    }
}

Output

This is line 1 written in file.
This is line 2 written in file.
This is line 3 written in file.
This is line 4 written in file.
This is line 5 written in file.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now