StreamReader.ReadToEnd StreamReader.ReadToEnd StreamReader.ReadToEnd StreamReader.ReadToEnd Method
定義
ストリームの現在位置から末尾までのすべての文字を読み込みます。Reads all characters from the current position to the end of the stream.
public:
override System::String ^ ReadToEnd();
public override string ReadToEnd ();
override this.ReadToEnd : unit -> string
Public Overrides Function ReadToEnd () As String
戻り値
ストリームの現在位置から末尾までの残りの部分 (文字列)。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.
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
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 をオーバーライドします。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
.NET Framework
.NET Standard
UWP
Xamarin.Android
Xamarin.iOS
Xamarin.Mac
こちらもご覧ください
フィードバック
お客様のご意見をお寄せください。 お寄せいただく内容の種類を選択:
このフィードバック システムは、GitHub Issues を利用して構築されています。 詳しくは、ブログをご覧ください。
フィードバックを読み込んでいます...