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 내부 버퍼와 크기가 같은 버퍼를 사용하는 것이 더 효율적입니다. 스트림이 생성될 때 버퍼의 크기가 지정되지 않은 경우 기본 크기는 4KB(4096바이트)입니다.

현재 메서드가 을 OutOfMemoryExceptionthrow하는 경우 기본 개체의 판독기 Stream 위치는 메서드가 읽을 수 있는 문자 수로 고급화되지만 내부 ReadLine 버퍼로 이미 읽은 문자는 삭제됩니다. 데이터를 버퍼로 읽은 후 기본 스트림의 위치를 조작하는 경우 기본 스트림의 위치가 내부 버퍼의 위치와 일치하지 않을 수 있습니다. 내부 버퍼를 다시 설정하려면 메서드를 DiscardBufferedData 호출합니다. 그러나 이 메서드는 성능이 느려지고 절대적으로 필요한 경우에만 호출해야 합니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

적용 대상

추가 정보