StreamReader.ReadLine メソッド
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在のストリームから 1 行分の文字を読み取り、そのデータを文字列として返します。
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 をオーバーライドします。
現在のメソッドが を OutOfMemoryExceptionスローする場合、基になる Stream オブジェクト内のリーダーの位置は、メソッドが読み取ることができる文字数だけ進みますが、内部 ReadLine バッファーに既に読み込まれている文字は破棄されます。 バッファーへのデータの読み取り後に基になるストリームの位置を操作すると、基になるストリームの位置が内部バッファーの位置と一致しない可能性があります。 内部バッファーをリセットするには、 メソッドを DiscardBufferedData 呼び出します。ただし、このメソッドはパフォーマンスを低下させ、絶対に必要な場合にのみ呼び出す必要があります。
共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。