StreamReader.ReadLine 메서드

정의

현재 스트림에서 한 줄의 문자를 읽고 데이터를 문자열로 반환합니다.

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

반환

입력 스트림의 다음 줄을 반환하거나 입력 스트림의 끝에 도달한 경우 null 을 반환합니다.

예외

메모리가 부족하여 반환된 문자열의 버퍼를 할당할 수 없습니다.

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
      {
         while ( sr->Peek() >= 0 )
         {
            Console::WriteLine( sr->ReadLine() );
         }
      }
      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))
            {
                while (sr.Peek() >= 0)
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        }
        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)

            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

설명

줄은 줄 바꿈("\n"), 캐리지 리턴("\r") 또는 줄 바꿈 바로 뒤에 줄 바꿈("\r\n")이 오는 문자 시퀀스로 정의됩니다. 반환되는 문자열에는 종료 캐리지 리턴 또는 줄 바꿈이 포함되어 있지 않습니다. 반환된 값은 null 입력 스트림의 끝에 도달하면 입니다.

이 메서드는 TextReader.ReadLine를 재정의합니다.

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

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

적용 대상

추가 정보