BinaryReader.ReadChar Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Membaca karakter berikutnya dari aliran saat ini dan memajukan posisi aliran saat ini sesuai dengan Encoding
karakter yang digunakan dan karakter tertentu yang dibaca dari aliran.
public:
virtual char ReadChar();
public virtual char ReadChar ();
abstract member ReadChar : unit -> char
override this.ReadChar : unit -> char
Public Overridable Function ReadChar () As Char
Mengembalikan
Karakter yang dibaca dari aliran saat ini.
Pengecualian
Akhir aliran tercapai.
Aliran ditutup.
Terjadi kesalahan I/O.
Karakter pengganti dibaca.
Contoh
Contoh kode berikut menunjukkan cara membaca dan menulis data menggunakan memori sebagai penyimpanan cadangan.
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);
}
}
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: "
for i = 0 to invalidPathChars.Length - 1 do
binWriter.Write invalidPathChars[i]
// 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()}"
let memoryData = Array.zeroCreate<char> (int (memStream.Length - memStream.Position))
for i = 0 to memoryData.Length - 1 do
memoryData[i] <- binReader.ReadChar()
printfn $"{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
Keterangan
ReadChar Jika metode mencoba membaca karakter pengganti dalam aliran, pengecualian akan dinaikkan dan posisi dalam aliran akan maju. Posisi dipulihkan ke lokasi asli sebelum ReadChar dipanggil jika aliran dapat dicari; namun, jika aliran tidak dapat dilihat, posisi tidak akan diperbaik. Jika karakter pengganti dapat diharapkan dalam aliran, gunakan metode sebagai gantinya ReadChars .
Karena konflik pemformatan data, menggunakan metode ini dengan pengodean berikut tidak disarankan:
UTF-7
ISO-2022-JP
ISCII
Untuk daftar tugas I/O umum, lihat Tugas I/O Umum.