StreamReader.ReadLine 方法

定义

从当前流中读取一行字符并将数据作为字符串返回。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

返回

输入流中的下一行;如果到达了输入流的末尾,则为 nullThe 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. 如果到达了输入流的末尾,则返回的值为 nullThe 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.

适用于

另请参阅