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

返回

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

如果当前方法引发,则 OutOfMemoryException 读取器在基础对象中的位置 Stream 将由该方法可以读取的字符数提前,但已读入内部缓冲区的字符将被 ReadLine 丢弃。 如果在将数据读入缓冲区后操作基础流的位置,则基础流的位置可能与内部缓冲区的位置不匹配。 若要重置内部缓冲区,请调用 DiscardBufferedData 方法; 但是,此方法会降低性能,只应在绝对必要的情况下调用。

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务

适用于

另请参阅