StreamReader.Read Method

Definition

Reads the next character or next set of characters from the input stream.

Overloads

Name Description
Read()

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

Read(Span<Char>)

Reads the characters from the current stream into a span.

Read(Char[], Int32, Int32)

Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.

Read()

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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

public:
 override int Read();
public override int Read();
override this.Read : unit -> int
Public Overrides Function Read () As Integer

Returns

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

Exceptions

An I/O error occurs.

Examples

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

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))
            {
                while (sr.Peek() >= 0)
                {
                    Console.Write((char)sr.Read());
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
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.

using System;
using System.IO;

class StrmRdrRead
{
public static void Main()
    {
    //Create a FileInfo instance representing an existing text file.
    FileInfo MyFile=new FileInfo(@"c:\csc.txt");
    //Instantiate a StreamReader to read from the text file.
    StreamReader sr=MyFile.OpenText();
    //Read a single character.
    int FirstChar=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();
    }
}
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
End Class

Remarks

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.

See also

Applies to

Read(Span<Char>)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

Reads the characters from the current stream into a span.

public:
 override int Read(Span<char> buffer);
public override int Read(Span<char> buffer);
override this.Read : Span<char> -> int
Public Overrides Function Read (buffer As Span(Of Char)) As Integer

Parameters

buffer
Span<Char>

When this method returns, contains the specified span of characters replaced by the characters read from the current source.

Returns

The number of characters that have been read, or 0 if at the end of the stream and no data was read. The number will be less than or equal to the buffer length, depending on whether the data is available within the stream.

Exceptions

The number of characters read from the stream is larger than the buffer length.

buffer is null.

Applies to

Read(Char[], Int32, Int32)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.

public:
 override int Read(cli::array <char> ^ buffer, int index, int count);
public override int Read(char[] buffer, int index, int count);
override this.Read : char[] * int * int -> int
Public Overrides Function Read (buffer As Char(), index As Integer, count As Integer) As Integer

Parameters

buffer