StreamReader.ReadToEnd メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

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


このメソッドは、TextReader.ReadToEnd をオーバーライドします。
ReadToEnd は、ストリームの現在位置から末尾までのすべての入力を読み込む必要があるときに最適です。ストリームから読み込む文字数についてさらに制御が必要な場合は、一般により高いパフォーマンスが得られる Read(Char[],Int32,Int32) メソッドを使用してください。
ReadToEnd では、末尾に到達したことをストリームが認識することを前提としています。サーバーがデータ送信を要求されたときにデータだけを送信して接続を閉じない対話型プロトコルでは、ReadToEnd によってブロックされたままになる場合があるため、使用しないでください。
Read メソッドを使用するときは、ストリームの内部バッファと同じサイズのバッファを使用すると効率的です。ストリームの生成時にバッファのサイズを指定しなかった場合は、既定値の 4 KB (4096 バイト) に設定されます。
現在のメソッドが OutOfMemoryException をスローした場合、基になる Stream オブジェクト内のリーダーの位置は読み取ることができた文字数分だけ進みますが、既に内部 ReadLine バッファに読み取られた文字は破棄されます。ストリーム内のリーダーの位置は変更できないため、既に読み取られた文字は復元できません。もう一度アクセスするには、StreamReader オブジェクトを再初期化する必要があります。ストリーム内の初期位置が不明であるか、ストリームがシークをサポートしない場合は、基になる Stream オブジェクトも再初期化する必要があります。
このような状況を回避し、信頼性の高いコードを作成するには、Read メソッドを使用して、割り当て済みのバッファに読み取った文字を格納する必要があります。
このメソッドの使用例については、「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
File.AppendText FileInfo.AppendText | |
FileInfo.Length | |
File.GetAttributes | |
File.SetAttributes | |

1 回の操作でファイルの末尾まで読み取るコード例を次に示します。
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
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()); } } }
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 ); } }
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 { //This allows you to do one Read operation. Console.WriteLine(sr.ReadToEnd()); } finally { sr.Dispose(); } } catch (System.Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } //main } //Test

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からStreamReader.ReadToEnd メソッドを検索する場合は、下記のリンクをクリックしてください。

- StreamReader.ReadToEnd メソッドのページへのリンク