StreamReader.ReadToEnd 方法

定义

读取来自流的当前位置到结尾的所有字符。

public:
 override System::String ^ ReadToEnd();
public override string ReadToEnd ();
override this.ReadToEnd : unit -> string
Public Overrides Function ReadToEnd () As String

返回

String

字符串形式的流的其余部分(从当前位置到结尾)。 如果当前位置位于流结尾,则返回空字符串 (“”)。

例外

没有足够的内存来为返回的字符串分配缓冲区。

出现 I/O 错误。

示例

下面的代码示例在一次操作中全部读取到文件末尾。

using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   try
   {
      if ( File::Exists( path ) )
      {
         File::Delete( path );
      }
      StreamWriter^ sw = gcnew StreamWriter( path );
      try
      {
         sw->WriteLine( "This" );
         sw->WriteLine( "is some text" );
         sw->WriteLine( "to test" );
         sw->WriteLine( "Reading" );
      }
      finally
      {
         delete sw;
      }

      StreamReader^ sr = gcnew StreamReader( path );
      try
      {
         //This allows you to do one Read operation.
         Console::WriteLine( sr->ReadToEnd() );
      }
      finally
      {
         delete sr;
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
using System;
using System.IO;

class Test
{
    
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }

            using (StreamReader sr = new StreamReader(path))
            {
                //This allows you to do one Read operation.
                Console.WriteLine(sr.ReadToEnd());
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO
Imports System.Text

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        Try
            If File.Exists(path) Then
                File.Delete(path)
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("This")
            sw.WriteLine("is some text")
            sw.WriteLine("to test")
            sw.WriteLine("Reading")
            sw.Close()

            Dim sr As StreamReader = New StreamReader(path)

            'This allows you to do one Read operation.
            Console.WriteLine(sr.ReadToEnd())
            sr.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

注解

此方法重写 TextReader.ReadToEnd

ReadToEnd 当你需要从流的当前位置到末尾读取所有输入时,效果最佳。 如果需要对从流中读取的字符进行更多的控制,请使用 Read(Char[], Int32, Int32) 方法重载,这通常会提高性能。

ReadToEnd 假定流知道何时到达结束。 对于交互式协议,服务器仅在请求时发送数据,并且不会关闭连接, ReadToEnd 因此可能会无限期阻止,因为它没有到达末尾,应避免这样做。

请注意 Read ,使用方法时,使用与流的内部缓冲区大小相同的缓冲区会更有效。 如果在构造流时未指定缓冲区的大小,则其默认大小为 4 kb (4096 字节) 。

如果当前方法引发,则 OutOfMemoryException 读取器在基础对象中的位置 Stream 将由该方法可以读取的字符数提前,但已读入内部缓冲区的字符将被 ReadLine 丢弃。 如果在将数据读入缓冲区后操作基础流的位置,则基础流的位置可能与内部缓冲区的位置不匹配。 若要重置内部缓冲区,请调用 DiscardBufferedData 方法; 但是,此方法会降低性能,只应在绝对必要的情况下调用。

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务

适用于

另请参阅