StreamReader.ReadToEnd 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 위치부터 스트림 끝까지의 모든 문자를 읽습니다.
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 바이트)입니다.
현재 메서드에서을 throw 하는 경우 OutOfMemoryException 내부 개체의 판독기 위치가 Stream 메서드에서 읽을 수 있었던 문자 수 만큼 이동 하지만 이미 내부 버퍼로 읽어온 문자는 ReadLine 삭제 됩니다. 버퍼에 데이터를 읽은 후 기본 스트림의 위치를 조작 하는 경우 내부 스트림의 위치가 내부 버퍼의 위치와 일치 하지 않을 수 있습니다. 내부 버퍼를 다시 설정 하려면 메서드를 호출 합니다 DiscardBufferedData . 그러나이 메서드는 성능을 저하 시키며 반드시 필요한 경우에만 호출 해야 합니다.
일반적인 i/o 작업 목록은 일반적인 I/o 작업을 참조 하세요.