BinaryReader.ReadChars(Int32) 方法

定義

從目前串流讀取指定數量的字元,回傳字元陣列中的資料,並根據所使用的字元及從串流讀取的特定字元推進當前位置 Encoding

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

參數

count
Int32

字數的數量。

傳回

Char[]

一個字元陣列,包含從底層串流讀取的資料。 如果串流結束時,這可能比請求的字元數還少。

例外狀況

需讀取的解碼字元數大於 count。 這種情況可能發生在 Unicode 解碼器回傳備用字元或代理字元對時。

溪流已經關閉。

發生 I/O 錯誤。

count 為負數。

範例

以下程式碼範例展示了如何利用記憶體作為後備儲存來讀寫資料。

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

備註

BinaryReader 讀取失敗後無法恢復檔案位置。

在從網路串流讀取時,在某些罕見情況下,若串流是BinaryReader使用 Unicode 編碼,該ReadChars方法可能會讀取額外的字元。 如果發生這種情況,你可以用這個ReadBytes方法讀取固定長度的位元組陣列,然後把這個陣列傳給方法。ReadChars

適用於

另請參閱