StreamReader.ReadToEnd Method

定義

ストリームの現在位置から末尾までのすべての文字を読み込みます。Reads all characters from the current position to the end of the stream.

C#
public override string ReadToEnd ();

戻り値

ストリームの現在位置から末尾までの残りの部分 (文字列)。The rest of the stream as a string, from the current position to the end. 現在の位置がストリームの末尾である場合は、空の文字列 ("") が返されます。If the current position is at the end of the stream, returns an empty string ("").

例外

返却された文字列にバッファーを割り当てるには、メモリが不足しています。There is insufficient memory to allocate a buffer for the returned string.

I/O エラーが発生します。An I/O error occurs.

次のコード例は、1 つの操作でファイルの最後まで読み取ります。The following code example reads all the way to the end of a file in one operation.

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)) 
            {
                //This allows you to do one Read operation.
                Console.WriteLine(sr.ReadToEnd());
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

注釈

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

ReadToEnd 現在の位置からストリームの末尾に、すべての入力を読み取る必要がある場合に最適です。ReadToEnd works best when you need to read all the input from the current position to the end of the stream. 文字数が、ストリームから読み取ら経由で詳細に制御が必要に応じて、使用して、Read(Char[], Int32, Int32)メソッド オーバー ロードには、パフォーマンスが向上します。If more control is needed over how many characters are read from the stream, use the Read(Char[], Int32, Int32) method overload, which generally results in better performance.

ReadToEnd 末尾に到達したときに、ストリームを知っていることを想定しています。ReadToEnd assumes that the stream knows when it has reached an end. 対話型のプロトコルを要求して、接続を閉じないする場合にのみ、サーバー データを送信ReadToEnd避ける必要があり、終わりに到達しませんので、無期限にブロックがあります。For interactive protocols in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely because it does not reach an end, and should be avoided.

使用する場合に注意して、Readメソッド、ストリームの内部バッファーと同じサイズであるバッファーを使用する方が効率的です。Note that when using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. ストリームが作成されるときに、バッファーのサイズを指定しなかった場合は、4 キロバイト (4,096 バイト) は、既定値。If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).

現在のメソッドがスローした場合、 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.

適用対象

.NET Core

3.0 Preview 2 2.2 2.1 2.0 1.1 1.0

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1

.NET Standard

2.0 1.6 1.5 1.4 1.3 1.2 1.1 1.0

UWP

10.0

Xamarin.Android

7.1

Xamarin.iOS

10.8

Xamarin.Mac

3.0

こちらもご覧ください