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
読み込まれる完全なファイルパス。
例外
例外の種類 | 条件 |
---|---|
path が空の文字列 ("") です。 |
|
path が null 参照 (Visual Basic では Nothing) です。 |
|
ファイルが見つかりません。 |
|
割り当てられていないドライブであるなど、指定されたパスが無効です。 |
|
path に、ファイル名、ディレクトリ名、またはボリューム ラベルとしては不正または無効な構文が含まれています。 |
解説
ファイルの絶対パスは、path パラメータで指定します。既定の文字エンコーディングと既定のバッファ サイズが使用されます。
path パラメータにはファイル名も指定できます。このファイルには、UNC 共有のファイルも含まれます。
path パラメータは、ディスクに格納されているファイルにする必要はありません。ストリームによるアクセスをサポートするシステム上の任意の部分を指定できます。
ヒント
特定のカルチャ設定で文字セットをコンパイルし、同じ文字を異なるカルチャ設定で取得すると、文字が正しく解釈されない場合があり、例外がスローされることもあります。
このメソッドの使用例については、「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
実行するタスク |
参考例があるトピック |
---|---|
テキスト ファイルを作成する。 |
|
テキスト ファイルに書き込む。 |
|
テキスト ファイルから読み取る。 |
|
テキストをファイルに追加する。 |
|
ファイルのサイズを取得する。 |
|
ファイルの属性を取得する。 |
|
ファイルの属性を設定する。 |
|
ファイルが存在するかどうかを判別する。 |
|
バイナリ ファイルから読み取る。 |
|
バイナリ ファイルに書き込む。 |
使用例
この 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