BinaryReader.ReadChars(Int32) 方法

定义

从当前流中读取指定的字符数,返回字符数组中的数据,并根据所使用的字符和从流中读取的特定字符推进当前位置 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 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构造了 Unicode 编码,此方法BinaryReader可能会从流中读取额外的字符。 如果发生这种情况,可以使用 ReadBytes 该方法读取固定长度字节数组,然后将该数组 ReadChars 传递给该方法。

适用于

另请参阅