BinaryReader.ReadChars(Int32) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Lê o número especificado de caracteres do fluxo atual, devolve os dados num array de caracteres e avança a posição atual de acordo com o Encoding utilizado e o carácter específico que está a ser lido do fluxo.
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()
Parâmetros
- count
- Int32
O número de caracteres para ler.
Devoluções
Um array de caracteres contendo dados lidos do fluxo subjacente. Isto pode ser inferior ao número de caracteres solicitados se o fim do stream for alcançado.
Exceções
O número de caracteres decodificados a ler é maior do que count. Isto pode acontecer se um descodificador Unicode devolver caracteres de reserva ou um par substituto.
O riacho está fechado.
Ocorreu um erro de I/O.
count é negativo.
Exemplos
O seguinte exemplo de código mostra como ler e escrever dados usando memória como armazenamento de apoio.
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
Observações
BinaryReader não restaura a posição do ficheiro após uma operação de leitura falhada.
Ao ler de fluxos de rede, em alguns casos raros, o ReadChars método pode ler um carácter extra do fluxo se este BinaryReader fosse construído com codificação Unicode. Se isto acontecer, pode usar o ReadBytes método para ler um array de bytes de comprimento fixo e depois passar esse array para o ReadChars método.