StreamReader.Read Method

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

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

'Declaration
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.

ExceptionCondition
IOException

An I/O error occurs.

This method overrides TextReader.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" 

        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
                Console.Write(Convert.ToChar(sr.Read()))
            Loop
            sr.Close()
        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)
      sr.Close()
   End Sub 'Main
End Class 'StrmRdrRead

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft