StreamReader クラス

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

この型のすべてのメンバの一覧については、StreamReader メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.IO.TextReader
         System.IO.StreamReader

<Serializable>
Public Class StreamReader   Inherits TextReader
[C#]
[Serializable]
public class StreamReader : TextReader
[C++]
[Serializable]
public __gc class StreamReader : public TextReader
[JScript]
public
   Serializable
class StreamReader extends TextReader

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

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

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

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

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

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

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

実行するタスク 参考例があるトピック
テキスト ファイルを作成する。 ファイルへのテキストの書き込み
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ファイルのサイズを取得する。 FileInfo.Length
ファイルの属性を取得する。 File.GetAttributes
ファイルの属性を設定する。 File.SetAttributes
ファイルが存在するかどうかを判別する。 File.Exists
バイナリ ファイルから読み取る。 新しく作成したデータ ファイルの読み取りと書き込み
バイナリ ファイルに書き込む。 新しく作成したデータ ファイルの読み取りと書き込み

使用例

[Visual Basic, C#, C++] 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

[C#] 
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);
        }
    }
}

[C++] 
#using <mscorlib.dll>

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 = new StreamReader(S"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) __try_cast<IDisposable*>(sr)->Dispose();
        }
    } catch (Exception* e) {
        // Let the user know what went wrong.
        Console::WriteLine(S"The file could not be read:");
        Console::WriteLine(e->Message);
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.IO

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

StreamReader メンバ | System.IO 名前空間 | Encoding | Stream | StreamWriter | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み | 基本のファイル I/O | 新しく作成したデータ ファイルの読み取りと書き込み