StreamReader コンストラクタ (String)

指定したファイル名用の StreamReader クラスの新しいインスタンスを初期化します。

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

構文

'宣言
Public Sub New ( _
    path As String _
)
'使用
Dim path As String

Dim instance As New StreamReader(path)
public StreamReader (
    string path
)
public:
StreamReader (
    String^ path
)
public StreamReader (
    String path
)
public function StreamReader (
    path : String
)

パラメータ

  • path
    読み込まれる完全なファイルパス。

例外

例外の種類 条件

ArgumentException

path が空の文字列 ("") です。

ArgumentNullException

path が null 参照 (Visual Basic では Nothing) です。

FileNotFoundException

ファイルが見つかりません。

DirectoryNotFoundException

割り当てられていないドライブであるなど、指定されたパスが無効です。

IOException

path に、ファイル名、ディレクトリ名、またはボリューム ラベルとしては不正または無効な構文が含まれています。

解説

ファイルの絶対パスは、path パラメータで指定します。既定の文字エンコーディングと既定のバッファ サイズが使用されます。

path パラメータにはファイル名も指定できます。このファイルには、UNC 共有のファイルも含まれます。

path パラメータは、ディスクに格納されているファイルにする必要はありません。ストリームによるアクセスをサポートするシステム上の任意の部分を指定できます。

ヒント

特定のカルチャ設定で文字セットをコンパイルし、同じ文字を異なるカルチャ設定で取得すると、文字が正しく解釈されない場合があり、例外がスローされることもあります。

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

実行するタスク

参考例があるトピック

テキスト ファイルを作成する。

方法 : ファイルにテキストを書き込む

テキスト ファイルに書き込む。

方法 : ファイルにテキストを書き込む

テキスト ファイルから読み取る。

方法 : ファイルからテキストを読み取る

テキストをファイルに追加する。

方法 : ログ ファイルを開いて情報を追加する

File.AppendText

FileInfo.AppendText

ファイルのサイズを取得する。

FileInfo.Length

ファイルの属性を取得する。

File.GetAttributes

ファイルの属性を設定する。

File.SetAttributes

ファイルが存在するかどうかを判別する。

File.Exists

バイナリ ファイルから読み取る。

方法 : 新しく作成されたデータ ファイルに対して読み書きする

バイナリ ファイルに書き込む。

方法 : 新しく作成されたデータ ファイルに対して読み書きする

使用例

この StreamReader コンストラクタのコード例を次に示します。

Imports System
Imports System.IO

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)

            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            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)) 
            {
                while (sr.Peek() >= 0) 
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        } 
        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
      {
         while ( sr->Peek() >= 0 )
         {
            Console::WriteLine( sr->ReadLine() );
         }
      }
      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 {
                while (sr.Peek() >= 0) {
                    Console.WriteLine(sr.ReadLine());
                }
            }
            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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

StreamReader クラス
StreamReader メンバ
System.IO 名前空間
Encoding

その他の技術情報

ファイルおよびストリーム入出力
方法 : ファイルからテキストを読み取る
方法 : ファイルにテキストを書き込む