BinaryReader.ReadChar BinaryReader.ReadChar BinaryReader.ReadChar BinaryReader.ReadChar Method

定義

現在のストリームの次の文字を読み取り、使用した Encoding とストリームから読み取った特定の文字に従ってストリームの現在位置を進めます。Reads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

public:
 virtual char ReadChar();
public virtual char ReadChar ();
abstract member ReadChar : unit -> char
override this.ReadChar : unit -> char
Public Overridable Function ReadChar () As Char

戻り値

現在のストリームから読み取った文字。A character read from the current stream.

例外

ストリームの末尾に到達しました。The end of the stream is reached.

ストリームは閉じられています。The stream is closed.

I/O エラーが発生しました。An I/O error occurred.

サロゲート文字が読み取られました。A surrogate character was read.

次のコード例は、バッキングストアとしてメモリを使用してデータの読み取りと書き込みを行う方法を示しています。The following code example shows how to read and write data using memory as a backing store.

using namespace System;
using namespace System::IO;
int main()
{
   int i;
   array<Char>^invalidPathChars = Path::InvalidPathChars;
   MemoryStream^ memStream = gcnew MemoryStream;
   BinaryWriter^ binWriter = gcnew BinaryWriter( memStream );
   
   // Write to memory.
   binWriter->Write( "Invalid file path characters are: " );
   for ( i = 0; i < invalidPathChars->Length; i++ )
   {
      binWriter->Write( invalidPathChars[ i ] );

   }
   
   // 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() );
   array<Char>^memoryData = gcnew array<Char>(memStream->Length - memStream->Position);
   for ( i = 0; i < memoryData->Length; i++ )
   {
      memoryData[ i ] = binReader->ReadChar();

   }
   Console::WriteLine( memoryData );
}

using System;
using System.IO;

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

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        for(i = 0; i < invalidPathChars.Length; i++)
        {
            binWriter.Write(invalidPathChars[i]);
        }

        // 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());
        char[] memoryData = 
            new char[memStream.Length - memStream.Position];
        for(i = 0; i < memoryData.Length; i++)
        {
            memoryData[i] = binReader.ReadChar();
        }
        Console.WriteLine(memoryData);
    }
}
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()
    
        Dim i As Integer = 0
        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: ")
        For i = 0 To invalidPathChars.Length - 1
            binWriter.Write(invalidPathChars(i))
        Next i

        ' 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())
        Dim memoryData( _
            CInt(memStream.Length - memStream.Position) - 1) As Char
        For i = 0 To memoryData.Length - 1
            memoryData(i) = binReader.ReadChar()
        Next i
        Console.WriteLine(memoryData)
    
    End Sub
End Class

注釈

ReadChar メソッドがストリーム内のサロゲート文字を読み取ろうとすると、例外が発生し、ストリーム内の位置が先に進みます。If the ReadChar method attempts to read a surrogate character in the stream an exception will be raised and the position in the stream will advance. ストリームがシーク可能な場合、ReadChar が呼び出される前に、位置が元の場所に復元されます。ただし、ストリームがシーク不可能な場合、位置は修正されません。The position is restored to the original location before ReadChar was called if the stream is seekable; however, if the stream is unseekable, the position will not be corrected. ストリームでサロゲート文字が想定される場合は、代わりに ReadChars メソッドを使用します。If surrogate characters can be expected in the stream, use the ReadChars method instead.

データ形式の競合が原因で、この方法を次のエンコーディングで使用することは推奨されません。Because of data formatting conflicts, using this method with the following encodings is not recommended:

  • UTF-7UTF-7

  • ISO-2022-JPISO-2022-JP

  • ISCIIISCII

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。For a list of common I/O tasks, see Common I/O Tasks.

適用対象

こちらもご覧ください