C# StreamReader StreamReader(Stream)

Description

StreamReader StreamReader(Stream) Initializes a new instance of the StreamReader class for the specified stream.

Syntax

StreamReader.StreamReader(Stream) has the following syntax.


public StreamReader(
  Stream stream
)

Parameters

StreamReader.StreamReader(Stream) has the following parameters.

  • stream - The stream to be read.

Example

The following code example demonstrates this StreamReader constructor.


//from  w  w  w. ja va2 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 (FileStream fs = new FileStream(path, FileMode.Open)) 
        {
            using (StreamReader sr = new StreamReader(fs)) 
            {
                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