StreamReader クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public Class StreamReader Inherits TextReader
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class StreamReader : public TextReader

StreamReader は、特定のエンコーディングの文字を入力する場合に使用します。一方、Stream クラスは、バイトの入出力に使用します。標準テキスト ファイルから情報の行を読み取るには、StreamReader を使用します。
他に指定がない場合、現行システムでは StreamReader は ANSI コード ページではなく、既定の UTF-8 エンコーディングに設定されます。UTF-8 は、Unicode 文字を正しく処理し、オペレーティング システムの各ローカライズ バージョンで一貫した結果を提供します。
既定では、StreamReader はスレッド セーフではありません。スレッド セーフ ラッパーについては、TextReader.Synchronized のトピックを参照してください。
Read(Char[],Int32,Int32) メソッドおよび Write(Char[],Int32,Int32) メソッドは、それぞれ、count パラメータで指定した文字数の読み込みと書き込みを行います。これらのメソッドは、count パラメータで指定したバイト数の読み取りと書き込みを行う BufferedStream.Read や BufferedStream.Write とは区別されます。BufferedStream は、バイト配列の全要素の読み取りと書き込みだけに使用します。
![]() |
---|
このクラスの使用例については、「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
File.AppendText FileInfo.AppendText | |
FileInfo.Length | |
File.GetAttributes | |
File.SetAttributes | |

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.MarshalByRefObject
System.IO.TextReader
System.IO.StreamReader


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- StreamReader クラスのページへのリンク