Share via


BinaryReader.ReadChars(Int32) Metode

Definisi

Membaca jumlah karakter yang ditentukan dari aliran saat ini, mengembalikan data dalam array karakter, dan memajukan posisi saat ini sesuai dengan karakter yang Encoding digunakan dan karakter tertentu yang dibaca dari aliran.

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()

Parameter

count
Int32

Jumlah karakter yang akan dibaca.

Mengembalikan

Char[]

Array karakter yang berisi data yang dibaca dari aliran yang mendasar. Ini mungkin kurang dari jumlah karakter yang diminta jika akhir aliran tercapai.

Pengecualian

Jumlah karakter yang didekodekan untuk dibaca lebih besar dari count. Ini dapat terjadi jika dekoder Unicode mengembalikan karakter fallback atau pasangan pengganti.

Aliran ditutup.

Terjadi kesalahan I/O.

Contoh

Contoh kode berikut menunjukkan cara membaca dan menulis data menggunakan memori sebagai penyimpanan cadangan.

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

Keterangan

BinaryReader tidak memulihkan posisi file setelah operasi baca yang gagal.

Saat membaca dari aliran jaringan, dalam beberapa kasus yang jarang terjadi, ReadChars metode mungkin membaca karakter tambahan dari aliran jika BinaryReader dibangun dengan pengodean Unicode. Jika ini terjadi, Anda dapat menggunakan ReadBytes metode untuk membaca array byte panjang tetap, lalu meneruskan array tersebut ke ReadChars metode .

Berlaku untuk

Lihat juga