C# StreamReader Read()

Description

StreamReader Read() Reads the next character from the input stream and advances the character position by one character.

Syntax

StreamReader.Read() has the following syntax.


public override int Read()

Returns

StreamReader.Read() method returns The next character from the input stream represented as an Int32 object, or -1 if no more characters are available.

Example

The following code example demonstrates a simple use of the Read method.


//from   w  ww.  ja 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 ");
            sw.WriteLine("java2s.com");
            sw.WriteLine(".");
        }

        using (StreamReader sr = new StreamReader(path)) 
        {
            while (sr.Peek() >= 0) 
            {
                Console.Write((char)sr.Read());
            }
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




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