C# StreamReader ReadBlock

Description

StreamReader ReadBlock Reads a specified maximum number of characters from the current stream and writes the data to a buffer, beginning at the specified index.

Syntax

StreamReader.ReadBlock has the following syntax.


public override int ReadBlock(
  char[] buffer,//from  w w w .  jav a 2 s .c  om
  int index,
  int count
)

Parameters

StreamReader.ReadBlock 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 position in buffer at which to begin writing.
  • count - The maximum number of characters to read.

Returns

StreamReader.ReadBlock method returns The number of characters that have been read. The number will be less than or equal to count, depending on whether all input characters have been read.

Example


//  w  ww.  ja v a 2  s  .c o 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.ReadBlock(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