StreamReader コンストラクタ (Stream)
指定したストリーム用の StreamReader クラスの新しいインスタンスを初期化します。
名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文
'宣言
Public Sub New ( _
stream As Stream _
)
'使用
Dim stream As Stream
Dim instance As New StreamReader(stream)
public StreamReader (
Stream stream
)
public:
StreamReader (
Stream^ stream
)
public StreamReader (
Stream stream
)
public function StreamReader (
stream : Stream
)
パラメータ
- stream
読み込まれるストリーム。
例外
例外の種類 | 条件 |
---|---|
stream が読み取りをサポートしていません。 |
|
stream が null 参照 (Visual Basic では Nothing) です。 |
解説
このコンストラクタは、UTF8Encoding へのエンコーディングを初期化し、stream パラメータを使用して BaseStream プロパティを初期化し、内部バッファを既定のサイズに初期化します。
ヒント
特定のカルチャ設定で文字セットをコンパイルし、同じ文字を異なるカルチャ設定で取得すると、文字が正しく解釈されない場合があり、例外がスローされることもあります。
このメソッドの使用例については、「使用例」を参照してください。その他の一般的な 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 fs As FileStream = New FileStream(path, FileMode.Open)
Dim sr As StreamReader = New StreamReader(fs)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
sr.Close()
fs.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 (FileStream fs = new FileStream(path, FileMode.Open))
{
using (StreamReader sr = new StreamReader(fs))
{
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;
}
FileStream^ fs = gcnew FileStream( path,FileMode::Open );
try
{
StreamReader^ sr = gcnew StreamReader( fs );
try
{
while ( sr->Peek() >= 0 )
{
Console::WriteLine( sr->ReadLine() );
}
}
finally
{
delete sr;
}
}
finally
{
delete fs;
}
}
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();
}
FileStream fs = new FileStream(path, FileMode.Open);
try {
StreamReader sr = new StreamReader(fs);
try {
while (sr.Peek() >= 0) {
Console.WriteLine(sr.ReadLine());
}
}
finally {
sr.Dispose();
}
}
finally {
fs.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 名前空間