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 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 작업을 참조하세요.