StreamReader.ReadToEnd メソッド

定義

ストリームの現在位置から末尾までのすべての文字を読み込みます。

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

戻り値

ストリームの現在位置から末尾までの残りの部分 (文字列)。 現在の位置がストリームの末尾である場合は、空の文字列 ("") が返されます。

例外

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

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

次のコード例では、1 回の操作でファイルの末尾までを読み取ります。

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
      {
         //This allows you to do one Read operation.
         Console::WriteLine( sr->ReadToEnd() );
      }
      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))
            {
                //This allows you to do one Read operation.
                Console.WriteLine(sr.ReadToEnd());
            }
        }
        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)

            'This allows you to do one Read operation.
            Console.WriteLine(sr.ReadToEnd())
            sr.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

注釈

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

ReadToEnd は、現在の位置からストリームの末尾までのすべての入力を読み取る必要がある場合に最適です。 ストリームから読み取る文字の数に対してより多くの制御が必要な場合は、 メソッドのオーバーロードを Read(Char[], Int32, Int32) 使用します。これにより、一般にパフォーマンスが向上します。

ReadToEnd は、ストリームがいつ終了したかを認識していることを前提としています。 サーバーがデータを送信する対話型プロトコルの場合は、要求したときにのみデータが送信され、接続が閉じられない場合、 ReadToEnd 末尾に到達しないため無期限にブロックされる可能性があるため、回避する必要があります。

メソッドを使用する場合は、ストリームの Read 内部バッファーと同じサイズのバッファーを使用する方が効率的であることに注意してください。 ストリームの構築時にバッファーのサイズが指定されていない場合、既定のサイズは 4 KB (4096 バイト) です。

現在のメソッドが を OutOfMemoryExceptionスローする場合、基になる Stream オブジェクト内のリーダーの位置は、メソッドが読み取ることができる文字数だけ進みますが、内部 ReadLine バッファーに既に読み込まれている文字は破棄されます。 バッファーへのデータの読み取り後に基になるストリームの位置を操作すると、基になるストリームの位置が内部バッファーの位置と一致しない可能性があります。 内部バッファーをリセットするには、 メソッドを DiscardBufferedData 呼び出します。ただし、このメソッドはパフォーマンスを低下させ、絶対に必要な場合にのみ呼び出す必要があります。

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

適用対象

こちらもご覧ください