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

Description

BinaryReader Read(Char[], Int32, Int32) Reads the specified number of characters from the stream, starting from a specified point in the character array.

Syntax

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


public virtual int Read(
  char[] buffer,/*from   w ww  . j a v  a  2 s. co  m*/
  int index,
  int count
)

Parameters

BinaryReader.Read(Char[], Int32, Int32) has the following parameters.

  • buffer - The buffer to read data into.
  • index - The starting point in the buffer at which to begin reading into the buffer.
  • count - The number of characters to read.

Returns

BinaryReader.Read(Char[], Int32, Int32) method returns The total number of characters read into the buffer. This might be less than the number of characters requested if that many characters are not currently available, or it might be zero if the end of the stream is reached.

Example

The following example shows how to read and write data using memory as a backing store.


/* w ww .  j  a  v  a 2s  . c o m*/
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        char[] invalidPathChars = Path.InvalidPathChars;
        MemoryStream memStream = new MemoryStream();
        BinaryWriter binWriter = new BinaryWriter(memStream);

        binWriter.Write(Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);

        BinaryReader binReader = new BinaryReader(memStream);

        memStream.Position = 0;

        Console.Write(binReader.ReadString());
        int arraySize = (int)(memStream.Length - memStream.Position);
        char[] memoryData = new char[arraySize];
        binReader.Read(memoryData, 0, arraySize);
        Console.WriteLine(memoryData);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




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