C# BinaryReader Read()

Description

BinaryReader Read() Reads characters from the underlying stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

Syntax

BinaryReader.Read() has the following syntax.


public virtual int Read()

Returns

BinaryReader.Read() method returns The next character from the input stream, or -1 if no characters are currently available.

Example

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


//from w  w w . jav a  2s. c  om
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        int i = 0;
        char[] myChars = new char[]{'a','v','c','d'};
        MemoryStream memStream = new MemoryStream();
        BinaryWriter binWriter = new BinaryWriter(memStream);

        for(i = 0; i < myChars.Length; i++)
        {
            binWriter.Write(myChars[i]);
        }

        BinaryReader binReader = new BinaryReader(memStream);

        Console.Write(binReader.ReadString());
        char[] memoryData = new char[memStream.Length - memStream.Position];
        for(i = 0; i < memoryData.Length; i++)
        {
            memoryData[i] = Convert.ToChar(binReader.Read());
        }
        Console.WriteLine(memoryData);
    }
}




















Home »
  C# Tutorial »
    System.IO »




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