StreamReader クラスとは? わかりやすく解説

StreamReader クラス

特定のエンコーディングバイト ストリーム読み込む TextReader を実装ます。

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

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class StreamReader
    Inherits TextReader
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class StreamReader : TextReader
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class StreamReader : public
 TextReader
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class StreamReader extends TextReader
SerializableAttribute 
ComVisibleAttribute(true) 
public class StreamReader extends
 TextReader
解説解説

StreamReader は、特定のエンコーディング文字入力する場合使用します一方Stream クラスは、バイト入出力使用します標準テキスト ファイルか情報の行を読み取るには、StreamReader使用します

他に指定ない場合現行システムでは StreamReaderANSI コード ページではなく既定UTF-8 エンコーディング設定されます。UTF-8 は、Unicode 文字正しく処理しオペレーティング システムの各ローカライズ バージョン一貫した結果提供します

既定では、StreamReaderスレッド セーフではありません。スレッド セーフ ラッパーについては、TextReader.Synchronized のトピック参照してください

Read(Char[],Int32,Int32) メソッドおよび Write(Char[],Int32,Int32) メソッドは、それぞれcount パラメータ指定した文字数読み込みと書き込み行います。これらのメソッドは、count パラメータ指定したバイト数の読み取りと書き込みを行う BufferedStream.Read や BufferedStream.Write とは区別されます。BufferedStream は、バイト配列の全要素読み取りと書き込みだけに使用します

メモメモ

Stream から読み取るときは、ストリーム内部バッファと同じサイズバッファ使用する効率的です。

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

使用例使用例

StreamReader オブジェクト使用してファイルかテキスト読み取るコード例次に示します

Imports System
Imports System.IO

Class Test
    Public Shared Sub Main()
        Try
            ' Create an instance of StreamReader to read from a file.
            Dim sr As StreamReader = New
 StreamReader("TestFile.txt")
            Dim line As String
            ' Read and display the lines from the file until the end
 
            ' of the file is reached.
            Do
                line = sr.ReadLine()
                Console.WriteLine(Line)
            Loop Until line Is
 Nothing
            sr.Close()
        Catch E As Exception
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(E.Message)
        End Try
    End Sub
End Class
using System;
using System.IO;

class Test 
{
    public static void Main()
 
    {
        try 
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt"))
 
            {
                String line;
                // Read and display lines from the file until the end
 of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
 
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e) 
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}
using namespace System;
using namespace System::IO;
int main()
{
   try
   {
      // Create an instance of StreamReader to read from a file.
      // The using statement also closes the StreamReader.
      StreamReader^ sr = gcnew StreamReader( "TestFile.txt" );
      try
      {
         String^ line;
         
         // Read and display lines from the file until the end of 
         // the file is reached.
         while ( line = sr->ReadLine() )
         {
            Console::WriteLine( line );
         }
      }
      finally
      {
         if ( sr )
            delete (IDisposable^)sr;
      }
   }
   catch ( Exception^ e ) 
   {
      // Let the user know what went wrong.
      Console::WriteLine( "The file could not be read:" );
      Console::WriteLine( e->Message );
   }
}
import System.*;
import System.IO.*;

class Test
{
    public static void main(String[]
 args)
    {
        try {            
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            StreamReader sr = new StreamReader("TestFile.txt");
            try {
                String line;
                // Read and display lines from the file until the end
 of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
 {
                    Console.WriteLine(line);
                }
            }
            finally {
                sr.Dispose();
            }            
        }
        catch (System.Exception e) {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.get_Message());
        }
    } //main
} //Test
継承階層継承階層
System.Object
   System.MarshalByRefObject
     System.IO.TextReader
      System.IO.StreamReader
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



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

辞書ショートカット

すべての辞書の索引

「StreamReader クラス」の関連用語

StreamReader クラスのお隣キーワード
検索ランキング

   

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



StreamReader クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS