StringReader.ReadToEnd 方法
定义
读取从当前位置到字符串的结尾的所有字符并将它们作为单个字符串返回。Reads all characters from the current position to the end of the string and returns them as a single string.
public:
override System::String ^ ReadToEnd();
public override string ReadToEnd ();
override this.ReadToEnd : unit -> string
Public Overrides Function ReadToEnd () As String
返回
从当前位置到基础字符串的结尾之间的内容。The content from the current position to the end of the underlying string.
例外
没有足够的内存来为返回的字符串分配缓冲区。There is insufficient memory to allocate a buffer for the returned string.
当前读取器已关闭。The current reader is closed.
示例
此代码示例是为类提供的更大示例的一部分 TextReader 。This code example is part of a larger example provided for the TextReader class.
void ReadText( TextReader^ textReader )
{
Console::WriteLine( "From {0} - {1}", textReader->GetType()->Name, textReader->ReadToEnd() );
}
static void ReadText(TextReader textReader)
{
Console.WriteLine("From {0} - {1}",
textReader.GetType().Name, textReader.ReadToEnd());
}
Shared Sub ReadText(aTextReader As TextReader)
Console.WriteLine("From {0} - {1}", _
aTextReader.GetType().Name, aTextReader.ReadToEnd())
End Sub
注解
此方法将重写 TextReader.ReadToEnd 方法。This method overrides the TextReader.ReadToEnd method.
如果当前方法引发,则 OutOfMemoryException 读取器在基础字符串中的位置将由该方法可以读取的字符数提前,但已读入内部缓冲区的字符将被 ReadToEnd 丢弃。If the current method throws an OutOfMemoryException, the reader's position in the underlying string is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadToEnd buffer are discarded. 由于不能更改读取器在字符串中的位置,因此,已读取的字符不可恢复,只能通过重新初始化来访问 StringReader 。Because the position of the reader in the string cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StringReader. 若要避免这种情况,请使用 Read 方法并在预分配的缓冲区中存储读取字符。To avoid such a situation, use the Read method and store the read characters in a preallocated buffer.
下表列出了其他典型或相关 i/o 任务的示例。The following table lists examples of other typical or related I/O tasks.
| 若要执行此操作...To do this... | 请参见本主题中的示例...See the example in this topic... |
|---|---|
| 创建文本文件。Create a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 写入文本文件。Write to a text file. | 如何:将文本写入文件How to: Write Text to a File |
| 读取文本文件。Read from a text file. | 如何:从文件中读取文本How to: Read Text from a File |
| 向文件追加文本。Append text to a file. | 如何:打开并追加到日志文件How to: Open and Append to a Log File File.AppendText FileInfo.AppendText |
| 获取文件的大小。Get the size of a file. | FileInfo.Length |
| 获取文件的属性。Get the attributes of a file. | File.GetAttributes |
| 设置文件的属性。Set the attributes of a file. | File.SetAttributes |
| 确定文件是否存在。Determine if a file exists. | File.Exists |
| 从二进制文件中读取。Read from a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |
| 写入二进制文件。Write to a binary file. | 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File |