C# Program to Read All Lines using StreamReader
Posted by Superadmin on August 13 2022 10:07:07

C# Program to Read All Lines using StreamReader

 

 

This is a C# Program to use streamreader to read entire line.

Problem Description

This C# Program Uses StreamReader to Read Entire Line.

Problem Solution

Here with the help of streamreader it reads and displays the entire line from the file.

Program/Source Code

Here is source code of the C# Program to Use StreamReader to Read Entire Line. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Use StreamReader to Read Entire Line
 */
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
public sealed class Program
{
    public static void Main()
    {
        Stream s = new FileStream(@"c:\sri\srip.txt", FileMode.Open);
        using (StreamReader sr = new StreamReader(s, Encoding.UTF8))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
            Console.ReadLine();
        }
    }
}
Program Explanation

This C# program is used to use StreamReader to read the entire line. It reads with the help of StreamReader, using while loop checks the line is not equal to null. If the condition is true, then execute the iteration of the loop. Print the entire line from the file.

 
Runtime Test Cases
 
StreamWriter writes text files. It enables easy and efficient text output. 
It is best placed in a using-statement to ensure it is removed 
from memory when no longer needed. 
It provides several constructors and many methods.