BinaryReader.ReadChars(Int32) Metoda

Definicja

Odczytuje określoną liczbę znaków z bieżącego strumienia, zwraca dane w tablicy znaków i przechodzi bieżącą pozycję zgodnie z Encoding używanym i określonym znakiem odczytywanym ze strumienia.

public:
 virtual cli::array <char> ^ ReadChars(int count);
public virtual char[] ReadChars (int count);
abstract member ReadChars : int -> char[]
override this.ReadChars : int -> char[]
Public Overridable Function ReadChars (count As Integer) As Char()

Parametry

count
Int32

Liczba znaków do odczytania.

Zwraca

Char[]

Tablica znaków zawierająca dane odczytane ze strumienia bazowego. Może to być mniej niż liczba żądanych znaków, jeśli osiągnięto koniec strumienia.

Wyjątki

Liczba zdekodowanych znaków do odczytania jest większa niż count. Może się to zdarzyć, jeśli dekoder Unicode zwraca znaki powrotu lub parę zastępczą.

Strumień jest zamknięty.

Wystąpił błąd we/wy.

count jest ujemna.

Przykłady

W poniższym przykładzie kodu pokazano, jak odczytywać i zapisywać dane przy użyciu pamięci jako magazynu zapasowego.

using namespace System;
using namespace System::IO;
int main()
{
   array<Char>^invalidPathChars = Path::InvalidPathChars;
   MemoryStream^ memStream = gcnew MemoryStream;
   BinaryWriter^ binWriter = gcnew BinaryWriter( memStream );
   
   // Write to memory.
   binWriter->Write( "Invalid file path characters are: " );
   binWriter->Write( Path::InvalidPathChars );
   
   // Create the reader using the same MemoryStream 
   // as used with the writer.
   BinaryReader^ binReader = gcnew 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() );
   Console::WriteLine( binReader->ReadChars( (int)(memStream->Length - memStream->Position) ) );
}
using System;
using System.IO;

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

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        binWriter.Write(Path.InvalidPathChars);

        // 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());
        Console.WriteLine(binReader.ReadChars(
            (int)(memStream.Length - memStream.Position)));
    }
}
open System.IO

let invalidPathChars = Path.GetInvalidPathChars()
let memStream = new MemoryStream()
let binWriter = new BinaryWriter(memStream)

// Write to memory.
binWriter.Write "Invalid file path characters are: "
binWriter.Write invalidPathChars

// Create the reader using the same MemoryStream
// as used with the writer.
let 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.
printf $"{binReader.ReadString()}"
printfn $"{binReader.ReadChars(int (memStream.Length - memStream.Position))}"
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()
    
        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: ")
        binWriter.Write(Path.InvalidPathChars)

        ' 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())
        Console.WriteLine(binReader.ReadChars( _
            CInt(memStream.Length - memStream.Position)))
    
    End Sub
End Class

Uwagi

BinaryReader nie przywraca położenia pliku po nieudanej operacji odczytu.

Podczas odczytywania ze strumieni sieciowych w niektórych rzadkich przypadkach ReadChars metoda może odczytać dodatkowy znak ze strumienia, jeśli BinaryReader został skonstruowany przy użyciu kodowania Unicode. W takim przypadku możesz użyć ReadBytes metody , aby odczytać tablicę bajtów o stałej długości, a następnie przekazać tę tablicę ReadChars do metody .

Dotyczy

Zobacz też