TextReader.ReadToEnd 方法

定义

读取从当前位置到文本读取器末尾的所有字符并将它们作为一个字符串返回。Reads all characters from the current position to the end of the text reader and returns them as one string.

public:
 virtual System::String ^ ReadToEnd();
public virtual string ReadToEnd ();
abstract member ReadToEnd : unit -> string
override this.ReadToEnd : unit -> string
Public Overridable Function ReadToEnd () As String

返回

String

一个包含从当前位置到文本读取器末尾的所有字符的字符串。A string that contains all characters from the current position to the end of the text reader.

例外

出现 I/O 错误。An I/O error occurs.

TextReader 是关闭的。The TextReader is closed.

没有足够的内存来为返回的字符串分配缓冲区。There is insufficient memory to allocate a buffer for the returned string.

下一行中的字符数大于 MaxValueThe number of characters in the next line is larger than MaxValue

注解

如果该方法引发 OutOfMemoryException 异常,则读取器在基础中的位置 Stream 将由该方法可以读取的字符数高级,但会丢弃已读入内部缓冲区的字符 ReadToEndIf the method throws an OutOfMemoryException exception, the reader's position in the underlying Stream is advanced by the number of characters the method was able to read, but the characters that were already read into the internal ReadToEnd buffer are discarded. 由于无法更改流中读取器的位置,已读取的字符不可恢复,只能通过重新初始化来访问 TextReaderBecause the position of the reader in the stream cannot be changed, the characters that were already read are unrecoverable and can be accessed only by reinitializing the TextReader. 如果流中的初始位置未知或流不支持查找,则 Stream 还需要重新初始化基础。If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized.

若要避免这种情况并生成可靠的代码,应使用 Read 方法并在预分配的缓冲区中存储读取字符。To avoid such a situation and produce robust code you should use the Read method and store the read characters in a preallocated buffer.

类是一个抽象类。The TextReader class is an abstract class. 因此,不要在代码中对其进行实例化。Therefore, you do not instantiate it in your code. 有关使用方法的示例 ReadToEnd ,请参见 StreamReader.ReadToEnd 方法。For an example of using the ReadToEnd method, see the StreamReader.ReadToEnd method.

适用于

另请参阅