This documentation is archived and is not being maintained.

StreamReader.Read Method (Char[], Int32, Int32)

.NET Framework 1.1

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

[Visual Basic]
Overrides Overloads Public Function Read( _
   <InteropServices.In(), _
   Out()> ByVal buffer() As Char, _
   ByVal index As Integer, _
   ByVal count As Integer _
) As Integer
public override int Read(
] char[] buffer,
 int index,
 int count
public: int Read(
] __wchar_t buffer __gc[],
 int index,
 int count
public override function Read(
   buffer : Char[],
 index : int,
 count : int
) : int;


When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.
The index of buffer at which to begin writing.
The maximum number of characters to read.

Return Value

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 count, depending on whether the data is available within the stream.


Exception Type Condition
ArgumentException The buffer length minus index is less than count.
ArgumentNullException buffer is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException index or count is negative.
IOException An I/O error occurs, such as the stream is closed.


This method overrides Read.

This method returns an integer so that it can return -1 if the end of the stream has been reached.

When using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. If the size of the internal buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).

This method returns after either count characters are read, or the end of the file is reached. ReadBlock is a blocking version of StreamReader.Read.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File



Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File


[Visual Basic, C#, C++] The following example reads five characters at a time until the end of the file is reached.

[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"

            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
                '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.
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

using System;
using System.IO;

class Test 
    public static void Main() 
        string path = @"c:\temp\MyTest.txt";

            if (File.Exists(path)) 

            using (StreamWriter sw = new StreamWriter(path)) 
                sw.WriteLine("is some text");
                sw.WriteLine("to test");

            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.
        catch (Exception e) 
            Console.WriteLine("The process failed: {0}", e.ToString());

#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";

    try {
        if (File::Exists(path)) {

        StreamWriter* sw = new StreamWriter(path);
        try {
            sw->WriteLine(S"is some text");
            sw->WriteLine(S"to test");
        } __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.
        } __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 Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

StreamReader Class | StreamReader Members | System.IO Namespace | StreamReader.Read Overload List | Working with I/O | Reading Text from a File | Writing Text to a File