StreamReader.ReadToEnd Yöntem
Tanım
Geçerli konumdan akışın sonuna kadar olan tüm karakterleri okur.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
Döndürülenler
Akışın geri kalanı, geçerli konumdan sonuna kadar bir dize olarak.The rest of the stream as a string, from the current position to the end. Geçerli konum akışın sonunda ise, boş bir dize ("") döndürür.If the current position is at the end of the stream, returns an empty string ("").
Özel durumlar
Döndürülen dize için arabellek ayırmaya yetecek bellek yok.There is insufficient memory to allocate a buffer for the returned string.
Bir G/Ç hatası oluşur.An I/O error occurs.
Örnekler
Aşağıdaki kod örneği, tek bir işlemde bir dosyanın sonuna kadar olan tüm yolu okur.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
Açıklamalar
Bu yöntem geçersiz kılınır TextReader.ReadToEnd .This method overrides TextReader.ReadToEnd.
ReadToEnd geçerli konumdan akışın sonuna kadar tüm girişi okumanız gerektiğinde en iyi şekilde işe yarar.ReadToEnd works best when you need to read all the input from the current position to the end of the stream. Akıştan kaç karakterlik okunan üzerinde daha fazla denetim gerekiyorsa, Read(Char[], Int32, Int32) genellikle daha iyi performans elde eden yöntem aşırı yüklemesini kullanın.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 akışın bir uca ulaştığında bildiği varsayılır.ReadToEnd assumes that the stream knows when it has reached an end. Sunucusunun yalnızca istediğinizde veri gönderdiği ve bağlantıyı kapatmadığı etkileşimli protokoller için, ReadToEnd bir uca ulaşmadığından ve kaçınılması gerektiğinden süresiz olarak engellenebilir.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.
ReadYöntemini kullanırken, akışın iç arabelleğiyle aynı boyutta olan bir arabelleğin kullanılması daha etkilidir.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. Akış oluşturulduğunda arabelleğin boyutu belirtilmemişse, varsayılan boyutu 4 kilobayttır (4096 bayt).If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).
Geçerli yöntem bir oluşturursa OutOfMemoryException , okuyucunun temel alınan nesnedeki konumu, Stream yöntemin okuyabildii karakter sayısına göre ilerleiyor, ancak iç arabellekte zaten okunan karakterler ReadLine atılır.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. Verileri arabelleğe okuduktan sonra temeldeki akışın konumunu değiştirirseniz, temel alınan akışın konumu iç arabelleğin konumuyla eşleşmeyebilir.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. Dahili arabelleği sıfırlamak için, DiscardBufferedData yöntemini çağırın; ancak, bu yöntem performansı yavaşlatır ve yalnızca kesinlikle gerekli olduğunda çağrılmalıdır.To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.
Ortak g/ç görevlerinin listesi için bkz. ortak g/ç görevleri.For a list of common I/O tasks, see Common I/O Tasks.