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 メソッドに渡すことができます。

適用対象

こちらもご覧ください