This documentation is archived and is not being maintained.

StreamReader.Read Method

Reads the next character from the input stream and advances the character position by one character.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

Public Overrides Function Read As Integer

Return Value

Type: System.Int32
The next character from the input stream represented as an Int32 object, or -1 if no more characters are available.


An I/O error occurs.

This method overrides Read.

This method returns an integer so that it can return -1 if the end of the stream has been reached. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.

For a list of common I/O tasks, see Common I/O Tasks.

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

Imports System
Imports System.IO
Imports System.Text

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

            If File.Exists(path) Then
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("is some text")
            sw.WriteLine("to test")

            Dim sr As StreamReader = New StreamReader(path)

            Do While sr.Peek() >= 0
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

The following code example demonstrates reading a single character using the Read method overload, formatting the ASCII integer output as decimal and hexadecimal.

Imports System
Imports System.IO

Class StrmRdrRead

   Public Shared Sub Main()
      'Create a FileInfo instance representing an existing text file.
      Dim MyFile As New FileInfo("c:\csc.txt")
      'Instantiate a StreamReader to read from the text file.
      Dim sr As StreamReader = MyFile.OpenText()
      'Read a single character.
      Dim FirstChar As Integer = sr.Read()
      'Display the ASCII number of the character read in both decimal and hexadecimal format.
      Console.WriteLine("The ASCII number of the first character read is {0:D} in decimal and {1:X} in hexadecimal.", FirstChar, FirstChar)
   End Sub 'Main
End Class 'StrmRdrRead

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.