Export (0) Print
Expand All

BinaryReader.Read Method

Reads characters from the underlying stream and advances the current position of the stream.

Overload List

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.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Read() As Integer
[C#] public virtual int Read();
[C++] public: virtual int Read();
[JScript] public function Read() : int;

Reads count bytes from the stream with index as the starting point in the byte array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Read(Byte(), Integer, Integer) As Integer
[C#] public virtual int Read(byte[], int, int);
[C++] public: virtual int Read(unsigned char __gc[], int, int);
[JScript] public function Read(Byte[], int, int) : int;

Reads count characters from the stream with index as the starting point in the character array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Read(Char(), Integer, Integer) As Integer
[C#] public virtual int Read(char[], int, int);
[C++] public: virtual int Read(__wchar_t __gc[], int, int);
[JScript] public function Read(Char[], int, int) : int;

Example

[Visual Basic, C#, C++] The following code example shows how to read and write data using memory as a backing store.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Read. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()
    
        Dim i As Integer = 0
        Dim invalidPathChars() As Char = Path.InvalidPathChars
        Dim memStream As new MemoryStream()
        Dim binWriter As New BinaryWriter(memStream)

        ' Write to memory.
        binWriter.Write("Invalid file path characters are: ")
        For i = 0 To invalidPathChars.Length - 1
            binWriter.Write(invalidPathChars(i))
        Next i

        ' Create the reader using the same MemoryStream 
        ' as used with the writer.
        Dim binReader As New BinaryReader(memStream)

        ' Set Position to the beginning of the stream.
        memStream.Position = 0

        ' Read the data from memory and write it to the console.
        Console.Write(binReader.ReadString())
        Dim memoryData( _
            CInt(memStream.Length - memStream.Position) - 1) As Char
        For i = 0 To memoryData.Length - 1
            memoryData(i) = Convert.ToChar(binReader.Read())
        Next i
        Console.WriteLine(memoryData)
    
    End Sub
End Class

[C#] 
using System;
using System.IO;

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

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        for(i = 0; i < invalidPathChars.Length; i++)
        {
            binWriter.Write(invalidPathChars[i]);
        }

        // Create the reader using the same MemoryStream 
        // as used with the writer.
        BinaryReader binReader = new BinaryReader(memStream);

        // Set Position to the beginning of the stream.
        memStream.Position = 0;

        // Read the data from memory and write it to the console.
        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);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

void main()
{
    int i;
    Char invalidPathChars __gc[] = Path::InvalidPathChars;
    MemoryStream* memStream = new MemoryStream();
    BinaryWriter* binWriter = new BinaryWriter(memStream);

    // Write to memory.
    binWriter->Write(S"Invalid file path characters are: ");
    for(i = 0; i < invalidPathChars->Length; i++)
    {
        binWriter->Write(invalidPathChars[i]);
    }

    // Create the reader using the same MemoryStream 
    // as used with the writer.
    BinaryReader* binReader = new BinaryReader(memStream);

    // Set Position to the beginning of the stream.
    binReader->BaseStream->Position = 0;

    // Read the data from memory and write it to the console.
    Console::Write(binReader->ReadString());
    Char memoryData __gc[] = 
        new Char __gc[memStream->Length - memStream->Position];
    for(i = 0; i < memoryData->Length; i++)
    {
        memoryData[i] = Convert::ToChar(binReader->Read());
    }
    Console::WriteLine(memoryData);
}

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

See Also

BinaryReader Class | BinaryReader Members | System.IO Namespace

Show:
© 2015 Microsoft