StreamReader.ReadToEnd メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > StreamReader.ReadToEnd メソッドの意味・解説 

StreamReader.ReadToEnd メソッド

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

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
解説解説

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

ReadToEnd は、ストリーム現在位置から末尾までのすべての入力読み込む必要があるときに最適です。ストリームから読み込む文字数についてさらに制御必要な場合は、一般により高いパフォーマンス得られる Read(Char[],Int32,Int32) メソッド使用してください

ReadToEnd では、末尾到達したことをストリーム認識することを前提としています。サーバーデータ送信要求されたときにデータだけを送信して接続閉じない対話型プロトコルでは、ReadToEnd によってブロックされたままになる場合があるため、使用しないください

Read メソッド使用するときは、ストリーム内部バッファと同じサイズバッファ使用する効率的です。ストリーム生成時にバッファサイズ指定しなかった場合は、既定値の 4 KB (4096 バイト) に設定されます。

現在のメソッドOutOfMemoryExceptionスローした場合、基になる Stream オブジェクト内のリーダー位置読み取ることができた文字数分だけ進みますが、既に内部 ReadLine バッファ読み取られ文字破棄されます。ストリーム内のリーダー位置変更できないため、既に読み取られ文字復元できません。もう一度アクセスするには、StreamReader オブジェクトを再初期化する必要がありますストリーム内の初期位置不明であるか、ストリームシークサポートしない場合は、基になる Stream オブジェクトも再初期化する必要があります

このような状況回避し信頼性の高いコード作成するには、Read メソッド使用して割り当て済みバッファ読み取った文字格納する必要があります

このメソッド使用例については、「使用例」を参照してくださいその他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します

使用例使用例

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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「StreamReader.ReadToEnd メソッド」の関連用語

StreamReader.ReadToEnd メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



StreamReader.ReadToEnd メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS