BinaryReader.ReadChar Metodo

Definizione

Legge il carattere successivo dal flusso corrente e sposta in avanti la posizione corrente del flusso secondo il valore Encoding usato e il carattere specifico letto dal flusso.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

Restituisce

Char

Carattere letto dal flusso corrente.A character read from the current stream.

Eccezioni

È stata raggiunta la fine del flusso.The end of the stream is reached.

Il flusso è chiuso.The stream is closed.

Si è verificato un errore di I/O.An I/O error occurred.

È stato letto un carattere surrogato.A surrogate character was read.

Esempio

Nell'esempio di codice seguente viene illustrato come leggere e scrivere dati utilizzando la memoria come archivio di backup.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

Commenti

Se il ReadChar metodo tenta di leggere un carattere surrogato nel flusso, viene generata un'eccezione e la posizione nel flusso viene spostata.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. La posizione viene ripristinata nel percorso originale prima della ReadChar chiamata a se il flusso è ricercabile. Tuttavia, se il flusso non è ricercabile, la posizione non verrà corretta.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. Se è possibile prevedere caratteri surrogati nel flusso, usare ReadChars invece il metodo.If surrogate characters can be expected in the stream, use the ReadChars method instead.

A causa dei conflitti di formattazione dei dati, non è consigliabile usare questo metodo con le codifiche seguenti:Because of data formatting conflicts, using this method with the following encodings is not recommended:

  • UTF-7UTF-7

  • ISO-2022-JPISO-2022-JP

  • ISCIIISCII

Per un elenco delle attività di I/O comuni, vedere attività di i/o comuni.For a list of common I/O tasks, see Common I/O Tasks.

Si applica a

Vedi anche