StreamReader.ReadToEnd 方法

定義

讀取從目前位置到資料流末端的所有字元。

public:
 override System::String ^ ReadToEnd();
public override string ReadToEnd ();
override this.ReadToEnd : unit -> string
Public Overrides Function ReadToEnd () As 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 工作

適用於

另請參閱