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
[C#]
public override int Read(
   [
   In,
   Out
] char[] buffer,
 int index,
 int count
);
[C++]
public: int Read(
   [
   In,
   Out
] __wchar_t buffer __gc[],
 int index,
 int count
);
[JScript]
public override function Read(
   buffer : Char[],
 index : int,
 count : int
) : int;

Parameters

buffer
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.
index
The index of buffer at which to begin writing.
count
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.

Exceptions

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.

Remarks

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

File.AppendText

FileInfo.AppendText

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

Example

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

        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 Language Filter in the upper-left corner of the page.

Requirements

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

Show: