.NET Framework クラス ライブラリ
StreamReader..::.ReadLine メソッド

更新 : 2007 年 11 月

現在のストリームから 1 行分の文字を読み取り、そのデータを文字列として返します。

名前空間 :  System.IO
アセンブリ :  mscorlib (mscorlib.dll 内)
構文

Visual Basic (宣言)
Public Overrides Function ReadLine As String
Visual Basic (使用法)
Dim instance As StreamReader
Dim returnValue As String

returnValue = instance.ReadLine()
C#
public override string ReadLine()
Visual C++
public:
virtual String^ ReadLine() override
J#
public String ReadLine()
JScript
public override function ReadLine() : String

戻り値

型 : System..::.String
入力ストリームからの次の行。入力ストリームの末尾に到達した場合は nullNothingnullptrnull 参照 (Visual Basic では Nothing)
例外

例外条件
OutOfMemoryException

返される文字列用のバッファを割り当てるためにはメモリが不足しています。

IOException

I/O エラーが発生しました。

解説

行は、ライン フィード ("\n")、キャリッジ リターン ("\r")、またはキャリッジ リターンの直後にライン フィードがある組み合わせが末尾に付いた一連の文字として定義されます。返される文字列には、末尾のキャリッジ リターンやライン フィードは含まれません。入力ストリームの末尾に到達した場合、戻り値は nullNothingnullptrnull 参照 (Visual Basic では Nothing) です。

このメソッドは、ReadLine をオーバーライドします。

現在のメソッドが OutOfMemoryException をスローした場合、基になる Stream オブジェクト内のリーダーの位置は読み取ることができた文字数分だけ進みますが、既に内部 ReadLine バッファに読み取られた文字は破棄されます。ストリーム内のリーダーの位置は変更できないため、既に読み取られた文字は復元できません。もう一度アクセスするには、StreamReader オブジェクトを再初期化する必要があります。ストリーム内の初期位置が不明であるか、ストリームがシークをサポートしない場合は、基になる Stream オブジェクトも再初期化する必要があります。

このような状況を回避し、信頼性の高いコードを作成するには、Read メソッドを使用して、割り当て済みのバッファに読み取った文字を格納する必要があります。

共通 I/O タスクの一覧については、「共通 I/O タスク」を参照してください。


ファイルの末尾に到達するまでファイルから行を読み取るコード例を次に示します。

Visual Basic
Imports System
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
C#
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());
        }
    }
}
Visual C++
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 );
   }
}
J#
import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        String path = "c:\\temp\\MyTest.txt";
        try {
            if (File.Exists(path)) {
                File.Delete(path);
            }
            StreamWriter sw = new StreamWriter(path);
            try {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }
            finally {
                sw.Dispose();
            }
            StreamReader sr = new StreamReader(path);
            try {
                while (sr.Peek() >= 0) {
                    Console.WriteLine(sr.ReadLine());
                }
            }
            finally {
                sr.Dispose();
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test
プラットフォーム

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
バージョン情報

.NET Framework

サポート対象 : 3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 3.5、2.0、1.0

XNA Framework

サポート対象 : 2.0、1.0
参照

参照

その他の技術情報

タグ :


Page view tracker