BinaryReader.ReadChars(Int32) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 스트림에서 지정된 문자 수만큼 읽어 문자 배열로 데이터를 반환하고, 사용된 Encoding과 스트림에서 읽어 오는 특정 문자의 길이만큼 현재 위치를 앞으로 이동합니다.
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()
매개 변수
- count
- Int32
읽을 문자 수입니다.
반환
내부 스트림에서 읽은 데이터를 포함하는 문자 배열입니다. 이 문자 배열은 스트림의 끝에 도달할 경우 요청된 문자 수보다 작을 수 있습니다.
예외
읽을 수 있도록 디코딩된 문자의 수는 count보다 큽니다. 유니코드 디코더가 대체 문자 또는 서로게이트 쌍을 반환하는 경우 이 문제가 발생할 수 있습니다.
스트림이 닫혔습니다.
I/O 오류가 발생했습니다.
count가 음수입니다.
예제
다음 코드 예제에서는 메모리를 백업 저장소로 사용하여 데이터를 읽고 쓰는 방법을 보여줍니다.
using namespace System;
using namespace System::IO;
int main()
{
array<Char>^invalidPathChars = Path::InvalidPathChars;
MemoryStream^ memStream = gcnew MemoryStream;
BinaryWriter^ binWriter = gcnew 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 = 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() );
Console::WriteLine( binReader->ReadChars( (int)(memStream->Length - memStream->Position) ) );
}
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
설명
BinaryReader 는 실패한 읽기 작업 후에 파일 위치를 복원하지 않습니다.
네트워크 스트림에서 읽을 때 드문 경우 ReadChars 에서 메서드는 유니코드 인코딩으로 생성된 경우 BinaryReader 스트림에서 추가 문자를 읽을 수 있습니다. 이 경우 메서드를 ReadBytes 사용하여 고정 길이 바이트 배열을 읽은 다음 해당 배열을 메서드에 ReadChars 전달할 수 있습니다.