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

注釈

BinaryReader は、読み取り操作が失敗した後にファイルの位置を復元しません。

ネットワーク ストリームから読み取る場合、まれに、 ReadChars が Unicode エンコードで構築されている場合 BinaryReader 、メソッドはストリームから余分な文字を読み取ることがあります。 これが発生した場合は、 メソッドを ReadBytes 使用して固定長バイト配列を読み取り、その配列を メソッドに ReadChars 渡すことができます。

適用対象

こちらもご覧ください