C# StreamReader Read(Char[], Int32, Int32)

Description

StreamReader Read(Char[], Int32, Int32) Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.

Syntax

StreamReader.Read(Char[], Int32, Int32) has the following syntax.


public override int Read(
  char[] buffer,//w w w  . j  a  v  a2 s.com
  int index,
  int count
)

Parameters

StreamReader.Read(Char[], Int32, Int32) has the following 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.

Returns

StreamReader.Read(Char[], Int32, Int32) method 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 count parameter, depending on whether the data is available within the stream.

Example

The following code example reads five characters at a time until the end of the file is reached.


//from w w w . j  a  va2s .co  m
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        using (StreamWriter sw = new StreamWriter(path)) 
        {
            sw.WriteLine("This");
            sw.WriteLine("is from ");
            sw.WriteLine("java2s.com");
            sw.WriteLine(".");
        }

        using (StreamReader sr = new StreamReader(path)) 
        {
            char[] c = null;
            while (sr.Peek() >= 0) 
            {
                c = new char[5];
                sr.Read(c, 0, c.Length);
                Console.WriteLine(c);
            }
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter