StreamReader.Read Method
.NET Framework 1.1
Reads the next character or next set of characters from the input stream.
Overload List
Reads the next character from the input stream and advances the character position by one character.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Overrides Public Function Read() As Integer
[C#] public override int Read();
[C++] public: int Read();
[JScript] public override function Read() : int;
Reads a maximum of count characters from the current stream into buffer, beginning at index.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Overrides Public Function Read(Char(), Integer, Integer) As Integer
[C#] public override int Read(char[], int, int);
[C++] public: int Read(__wchar_t __gc[], int, int);
[JScript] public override function Read(Char[], int, int) : int;
Example
[Visual Basic, C#, C++] The following example reads five characters at a time until the end of the file is reached.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Read. For other examples that might be available, see the individual overload topics.
[Visual Basic] Imports System Imports System.IO Imports System.Text Public Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" Try If File.Exists(path) Then File.Delete(path) End If Dim sw As StreamWriter = New StreamWriter(path) sw.WriteLine("This") sw.WriteLine("is some text") sw.WriteLine("to test") sw.WriteLine("Reading") sw.Close() Dim sr As StreamReader = New StreamReader(path) Do While sr.Peek() >= 0 'This is an arbitrary size for this example. Dim c(5) As Char sr.Read(c, 0, c.Length) 'The output will look odd, because 'only five characters are read at a time. Console.WriteLine(c) Loop sr.Close() Catch e As Exception Console.WriteLine("The process failed: {0}", e.ToString()) End Try End Sub End Class [C#] using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; try { if (File.Exists(path)) { File.Delete(path); } using (StreamWriter sw = new StreamWriter(path)) { sw.WriteLine("This"); sw.WriteLine("is some text"); sw.WriteLine("to test"); sw.WriteLine("Reading"); } using (StreamReader sr = new StreamReader(path)) { //This is an arbitrary size for this example. char[] c = null; while (sr.Peek() >= 0) { c = new char[5]; sr.Read(c, 0, c.Length); //The output will look odd, because //only five characters are read at a time. Console.WriteLine(c); } } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::IO; int main() { String* path = S"c:\\temp\\MyTest.txt"; try { if (File::Exists(path)) { File::Delete(path); } StreamWriter* sw = new StreamWriter(path); try { sw->WriteLine(S"This"); sw->WriteLine(S"is some text"); sw->WriteLine(S"to test"); sw->WriteLine(S"Reading"); } __finally { if (sw) __try_cast<IDisposable*>(sw)->Dispose(); } StreamReader* sr = new StreamReader(path); try { //This is an arbitrary size for this example. Char c[] = 0; while (sr->Peek() >= 0) { c = new Char[5]; sr->Read(c, 0, c->Length); //The output will look odd, because //only five characters are read at a time. Console::WriteLine(c); } } __finally { if (sr) __try_cast<IDisposable*>(sr)->Dispose(); } } catch (Exception* e) { Console::WriteLine(S"The process failed: {0}", e); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
See Also
StreamReader Class | StreamReader Members | System.IO Namespace