BinaryReader.ReadChars(Int32) Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Beolvassa a megadott számú karaktert az aktuális streamből, visszaadja az adatokat egy karaktertömbben, és az aktuális pozíciót a Encoding használt és a streamből beolvasott karakternek megfelelően alakítja tovább.
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()
Paraméterek
- count
- Int32
Az elolvasandó karakterek száma.
Válaszok
A mögöttes adatfolyamból beolvasott adatokat tartalmazó karaktertömb. Ez kisebb lehet, mint a kért karakterek száma, ha a stream vége el van érve.
Kivételek
Az olvasandó dekódolt karakterek száma nagyobb, mint counta . Ez akkor fordulhat elő, ha egy Unicode-dekódoló tartalék karaktereket vagy helyettesítő párokat ad vissza.
A stream bezárult.
I/O-hiba történt.
count negatív.
Példák
Az alábbi példakód bemutatja, hogyan olvashat és írhat adatokat memória használatával háttértárként.
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
Megjegyzések
BinaryReader sikertelen olvasási művelet után nem állítja vissza a fájl pozícióját.
Hálózati streamekből való olvasáskor a metódus néhány ritka esetben egy további karaktert is beolvashat a streamből, ReadChars ha unicode BinaryReader kódolással lett létrehozva. Ha ez történik, a ReadBytes metódussal beolvashat egy rögzített hosszúságú bájttömböt, majd átadhatja a tömböt a ReadChars metódusnak.