StreamReader.ReadToEnd Metoda

Definice

Přečte všechny znaky z aktuální pozice na konec proudu.Reads all characters from the current position to the end of the stream.

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

Návraty

String

Zbytek streamu jako řetězec, od aktuální pozice do konce.The rest of the stream as a string, from the current position to the end. Pokud je aktuální pozice na konci datového proudu, vrátí prázdný řetězec ("").If the current position is at the end of the stream, returns an empty string ("").

Výjimky

Není dostatek paměti k přidělení vyrovnávací paměti pro vrácený řetězec.There is insufficient memory to allocate a buffer for the returned string.

Dojde k vstupně-výstupní chybě.An I/O error occurs.

Příklady

Následující příklad kódu čte celý postup na konec souboru v jedné operaci.The following code example reads all the way to the end of a file in one operation.

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

Poznámky

Tato metoda přepisuje TextReader.ReadToEnd .This method overrides TextReader.ReadToEnd.

ReadToEnd funguje nejlépe v případě, že potřebujete číst všechny vstupy z aktuální pozice na konci streamu.ReadToEnd works best when you need to read all the input from the current position to the end of the stream. Pokud je potřeba více ovládacích prvků, kolik znaků je čteno z datového proudu, použijte Read(Char[], Int32, Int32) přetížení metody, což obecně vede k lepšímu výkonu.If more control is needed over how many characters are read from the stream, use the Read(Char[], Int32, Int32) method overload, which generally results in better performance.

ReadToEnd předpokládá, že datový proud ví, že se dosáhlo konce.ReadToEnd assumes that the stream knows when it has reached an end. U interaktivních protokolů, ve kterých Server odesílá data pouze v případě, že pro něj požádáte a neukončí připojení, ReadToEnd může blokovat neomezenou dobu, protože nedosáhne konce a je třeba se jim vyhnout.For interactive protocols in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely because it does not reach an end, and should be avoided.

Všimněte si, že při použití Read metody je efektivnější použít vyrovnávací paměť, která má stejnou velikost jako vnitřní vyrovnávací paměť datového proudu.Note that when using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. Pokud velikost vyrovnávací paměti nebyla určena při vytvoření datového proudu, jeho výchozí velikost je 4 kilobajty (4096 bajtů).If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).

Pokud aktuální metoda vyvolá výjimku OutOfMemoryException , pozice čtečky v podkladovém Stream objektu je rozšířena počtem znaků, které metoda byla schopna číst, ale znaky již čteny do vnitřní ReadLine vyrovnávací paměti jsou zahozeny.If the current method throws an OutOfMemoryException, the reader's position in the underlying Stream object is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. Pokud pracujete s umístěním podkladového datového proudu po čtení dat do vyrovnávací paměti, umístění podkladového datového proudu se nemusí shodovat s pozicí vnitřní vyrovnávací paměti.If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. Chcete-li obnovit vnitřní vyrovnávací paměť, zavolejte DiscardBufferedData metodu. Tato metoda však zpomaluje výkon a měla by být volána pouze v případě nezbytně nutné.To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.

Seznam běžných vstupně-výstupních úloh najdete v tématu běžné vstupně-výstupní úlohy.For a list of common I/O tasks, see Common I/O Tasks.

Platí pro