C# StreamReader StreamReader(String)

Description

StreamReader StreamReader(String) Initializes a new instance of the StreamReader class for the specified file name.

Syntax

StreamReader.StreamReader(String) has the following syntax.


public StreamReader(
  string path
)

Parameters

StreamReader.StreamReader(String) has the following parameters.

  • path - The complete file path to be read.

Example

The following code example demonstrates this StreamReader constructor.


//from w  w  w. j a v  a 2  s .c  om
using System;
using System.IO;

class Test 
{  
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        using (StreamWriter sw = new StreamWriter(path)) 
        {
            sw.WriteLine("This");
            sw.WriteLine("is from java2s.com");
        }

        using (StreamReader sr = new StreamReader(path)) 
        {
            while (sr.Peek() >= 0) 
            {
                Console.WriteLine(sr.ReadLine());
            }
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter