StreamReader.ReadLine StreamReader.ReadLine StreamReader.ReadLine StreamReader.ReadLine Method

定義

自目前資料流讀取一行字元,並將資料以字串傳回。Reads a line of characters from the current stream and returns the data as a string.

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

傳回

輸入資料流的下一行,或為 null (如果已到達輸入資料流末端)。The next line from the input stream, or null if the end of the input stream is reached.

例外狀況

沒有足夠記憶體可為傳回的字串配置緩衝區。There is insufficient memory to allocate a buffer for the returned string.

發生 I/O 錯誤。An I/O error occurs.

範例

下列程式碼範例會從檔案讀取行,直到到達檔案結尾為止。The following code example reads lines from a file until the end of the file is reached.

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")後面的回車。A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). 傳回的字串不包含終止的回車符或換行字元。The string that is returned does not contain the terminating carriage return or line feed. 如果到達輸入資料流程null的結尾,則傳回的值是。The returned value is null if the end of the input stream is reached.

這個方法覆寫 TextReader.ReadLineThis method overrides TextReader.ReadLine.

如果目前的方法OutOfMemoryException擲回,則讀取器在基礎Stream物件中的位置會依方法可讀取的字元數來增加,但已讀取至內部ReadLine緩衝區的字元會被捨棄.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. 如果您在將資料讀入緩衝區之後操作基礎資料流程的位置,基礎資料流程的位置可能不符合內部緩衝區的位置。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. 若要重設內部緩衝區, 請DiscardBufferedData呼叫方法; 不過, 這個方法會降低效能, 而且只有在絕對必要時才應呼叫。To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

適用於

另請參閱