C# StreamReader Peek
Description
StreamReader Peek
Returns the next available character
but does not consume it.
Syntax
StreamReader.Peek
has the following syntax.
public override int Peek()
Returns
StreamReader.Peek
method returns An integer representing the next character to be read, or -1 if there are no
characters to be read or if the stream does not support seeking.
Example
The following code example reads lines from a file until the end of the file is reached.
// w ww.j ava 2s . 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() > -1)
{
Console.WriteLine(sr.ReadLine());
}
}
}
}