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
要读取的字符数。
返回
Char[]
包含从基础流中读取的数据的字节数组。 如果到达了流的末尾,则该字符数组可能小于所请求的字符数。
例外
要读取的解码字符数大于 count
。 如果 Unicode 解码器返回回退字符或代理项对,则可能发生此情况。
流已关闭。
出现 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 读取操作失败后,不会还原文件位置。
从网络流读取时,在极少数情况下,如果 BinaryReader 是使用 Unicode 编码构造的,ReadChars则 方法可能会从流中读取额外的字符。 如果发生这种情况,可以使用 ReadBytes 方法读取固定长度的字节数组,然后将该数组传递给 ReadChars 方法。