次の方法で共有


StreamReader コンストラクター

定義

指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

オーバーロード

名前 説明
StreamReader(Stream)

指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

StreamReader(Stream, Encoding, Boolean, Int32, Boolean)

指定した文字エンコード、バイトオーダー マーク検出オプション、およびバッファー サイズに基づいて、指定したストリームの StreamReader クラスの新しいインスタンスを初期化し、必要に応じてストリームを開いたままにします。

StreamReader(String, Encoding, Boolean, FileStreamOptions)

指定したファイル パスの StreamReader クラスの新しいインスタンスを、指定した文字エンコード、バイトオーダー マーク検出オプションを使用して初期化し、指定した FileStreamOptions オブジェクトで構成します。

StreamReader(String, Encoding, Boolean, Int32)

指定した文字エンコード、バイトオーダー マーク検出オプション、バッファー サイズを使用して、指定したファイル名の StreamReader クラスの新しいインスタンスを初期化します。

StreamReader(Stream, Encoding, Boolean, Int32)

指定した文字エンコード、バイトオーダー マーク検出オプション、バッファー サイズを使用して、指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

StreamReader(Stream, Encoding, Boolean)

指定した文字エンコードとバイトオーダー マーク検出オプションを使用して、指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

StreamReader(String, Encoding, Boolean)

指定した文字エンコードとバイトオーダー マーク検出オプションを使用して、指定したファイル名の StreamReader クラスの新しいインスタンスを初期化します。

StreamReader(String, FileStreamOptions)

既定のエンコードを使用して、指定したファイル パスの StreamReader クラスの新しいインスタンスを初期化し、ファイルの先頭にあるバイトオーダー マークの検出を有効にし、指定した FileStreamOptions オブジェクトで構成します。

StreamReader(String, Boolean)

指定したバイト オーダー マーク検出オプションを使用して、指定したファイル名の StreamReader クラスの新しいインスタンスを初期化します。

StreamReader(Stream, Encoding)

指定した文字エンコードを使用して、指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

StreamReader(Stream, Boolean)

指定したバイトオーダー マーク検出オプションを使用して、指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

StreamReader(String)

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

StreamReader(String, Encoding)

指定した文字エンコードを使用して、指定したファイル名の StreamReader クラスの新しいインスタンスを初期化します。

StreamReader(Stream)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

public:
 StreamReader(System::IO::Stream ^ stream);
public StreamReader(System.IO.Stream stream);
new System.IO.StreamReader : System.IO.Stream -> System.IO.StreamReader
Public Sub New (stream As Stream)

パラメーター

stream
Stream

読み取るストリーム。

例外

stream は読み取りをサポートしていません。

streamnullです。

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

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(new FileStream(path, FileMode.CreateNew)))
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("reading");
            }

            using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open)))
            {
                while (sr.Peek() >= 0)
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
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

注釈

このコンストラクターは、UTF8Encodingへのエンコード、stream パラメーターを使用した BaseStream プロパティ、および内部バッファー サイズを 1024 バイトに初期化します。

StreamReader オブジェクトは、StreamReader.Disposeが呼び出されたときに、指定されたStream オブジェクトに対してDispose()を呼び出します。

注意事項

特定のカルチャ設定で文字のセットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

StreamReader(Stream, Encoding, Boolean, Int32, Boolean)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定した文字エンコード、バイトオーダー マーク検出オプション、およびバッファー サイズに基づいて、指定したストリームの StreamReader クラスの新しいインスタンスを初期化し、必要に応じてストリームを開いたままにします。

public:
 StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen);
public StreamReader(System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen);
public StreamReader(System.IO.Stream stream, System.Text.Encoding? encoding = default, bool detectEncodingFromByteOrderMarks = true, int bufferSize = -1, bool leaveOpen = false);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool * int * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer, leaveOpen As Boolean)
Public Sub New (stream As Stream, Optional encoding As Encoding = Nothing, Optional detectEncodingFromByteOrderMarks As Boolean = true, Optional bufferSize As Integer = -1, Optional leaveOpen As Boolean = false)

パラメーター

stream
Stream

読み取るストリーム。

encoding
Encoding

使用する文字エンコード。

detectEncodingFromByteOrderMarks
Boolean

true ファイルの先頭でバイトオーダーマークを検索する場合。それ以外の場合は false

bufferSize
Int32

最小バッファー サイズ (バイト単位)。

leaveOpen
Boolean

true StreamReader オブジェクトが破棄された後にストリームを開いたままにする場合は。それ以外の場合はfalse

注釈

leaveOpen パラメーターを true に設定しない限り、StreamReader オブジェクトは、StreamReader.Disposeが呼び出されたときに、指定されたStream オブジェクトでDispose()を呼び出します。

バッファー サイズ (バイト単位) は、 bufferSize パラメーターによって設定されます。 bufferSizeが最小許容サイズ (128 バイト) より小さい場合は、最小許容サイズが使用されます。

このコンストラクターを使用すると、 StreamReader オブジェクトから初めて読み取る際にエンコードを変更できます。 detectEncodingFromByteOrderMarks パラメーターがtrue場合、コンストラクターはストリームの最初の 4 バイトを調べることでエンコードを検出します。 ファイルが適切なバイトオーダー マークで始まる場合、UTF-8、リトル エンディアン UTF-16、ビッグ エンディアン UTF-16、リトル エンディアン UTF-32、ビッグ エンディアン UTF-32 テキストが自動的に認識されます。 それ以外の場合は、ユーザー指定のエンコードが使用されます。 詳細については、 Encoding.GetPreamble メソッドを参照してください。

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

注意事項

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

適用対象

StreamReader(String, Encoding, Boolean, FileStreamOptions)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定したファイル パスの StreamReader クラスの新しいインスタンスを、指定した文字エンコード、バイトオーダー マーク検出オプションを使用して初期化し、指定した FileStreamOptions オブジェクトで構成します。

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, System::IO::FileStreamOptions ^ options);
public StreamReader(string path, System.Text.Encoding? encoding, bool detectEncodingFromByteOrderMarks, System.IO.FileStreamOptions options);
public StreamReader(string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, System.IO.FileStreamOptions options);
new System.IO.StreamReader : string * System.Text.Encoding * bool * System.IO.FileStreamOptions -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, options As FileStreamOptions)

パラメーター

path
String

読み取る完全なファイル パス。

encoding
Encoding

使用する文字エンコード。

detectEncodingFromByteOrderMarks
Boolean

ファイルの先頭でバイト順マークを検索するかどうかを示します。

options
FileStreamOptions

基になる FileStreamの構成オプションを指定するオブジェクト。

例外

path は読み取り不可能です。

      -or-

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

path または encoding または optionsnull

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

マップされていないドライブ上など、指定されたパスが無効です。

path には、ファイル名、ディレクトリ名、またはボリューム ラベルの構文が正しくないか無効です。

こちらもご覧ください

適用対象

StreamReader(String, Encoding, Boolean, Int32)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定した文字エンコード、バイトオーダー マーク検出オプション、バッファー サイズを使用して、指定したファイル名の StreamReader クラスの新しいインスタンスを初期化します。

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader(string path, System.Text.Encoding? encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader(string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
new System.IO.StreamReader : string * System.Text.Encoding * bool * int -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)

パラメーター

path
String

読み取る完全なファイル パス。

encoding
Encoding

使用する文字エンコード。

detectEncodingFromByteOrderMarks
Boolean

ファイルの先頭でバイト順マークを検索するかどうかを示します。

bufferSize
Int32

最小バッファー サイズ (バイト単位)。

例外

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

path または encodingnull

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

マップされていないドライブ上など、指定されたパスが無効です。

path には、ファイル名、ディレクトリ名、またはボリューム ラベルの構文が正しくないか無効です。

bufferSize は 0 以下です。ただし、-1 を除き、既定のバッファー サイズを指定できます。

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

private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

注釈

このコンストラクターは、 encoding パラメーターで指定されたエンコードを初期化します。

このコンストラクターを使用すると、 StreamReader オブジェクトから初めて読み取る際にエンコードを変更できます。 detectEncodingFromByteOrderMarks パラメーターは、ストリームの最初の 4 バイトを調べることでエンコードを検出します。 ファイルが適切なバイトオーダー マークで始まる場合、UTF-8、リトル エンディアン UTF-16、ビッグ エンディアン UTF-16、リトル エンディアン UTF-32、ビッグ エンディアン UTF-32 テキストが自動的に認識されます。 それ以外の場合は、ユーザー指定のエンコードが使用されます。 詳細については、 Encoding.GetPreamble メソッドを参照してください。

バッファー サイズ (バイト単位) は、 bufferSize パラメーターによって設定されます。 bufferSizeが最小許容サイズ (128 バイト) より小さい場合は、最小許容サイズが使用されます。

path パラメーターには、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

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

注意事項

特定のカルチャ設定で文字のセットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

StreamReader(Stream, Encoding, Boolean, Int32)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定した文字エンコード、バイトオーダー マーク検出オプション、バッファー サイズを使用して、指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

public:
 StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader(System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader(System.IO.Stream stream, System.Text.Encoding? encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool * int -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)

パラメーター

stream
Stream

読み取るストリーム。

encoding
Encoding

使用する文字エンコード。

detectEncodingFromByteOrderMarks
Boolean

ファイルの先頭でバイト順マークを検索するかどうかを示します。

bufferSize
Int32

最小バッファー サイズ (バイト単位)。

例外

ストリームは読み取りをサポートしていません。

stream または encodingnull

bufferSize は 0 以下です。ただし、-1 を除き、既定のバッファー サイズを指定できます。

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

private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

注釈

バッファー サイズ (バイト単位) は、 bufferSize パラメーターによって設定されます。 bufferSizeが最小許容サイズ (128 バイト) より小さい場合は、最小許容サイズが使用されます。

このコンストラクターを使用すると、 StreamReader オブジェクトから初めて読み取る際にエンコードを変更できます。 detectEncodingFromByteOrderMarks パラメーターは、ストリームの最初の 4 バイトを調べることでエンコードを検出します。 ファイルが適切なバイトオーダー マークで始まる場合、UTF-8、リトル エンディアン UTF-16、ビッグ エンディアン UTF-16、リトル エンディアン UTF-32、ビッグ エンディアン UTF-32 テキストが自動的に認識されます。 それ以外の場合は、ユーザー指定のエンコードが使用されます。 詳細については、 Encoding.GetPreamble メソッドを参照してください。

StreamReader オブジェクトは、StreamReader.Disposeが呼び出されたときに、指定されたStream オブジェクトに対してDispose()を呼び出します。

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

注意事項

特定のカルチャ設定で文字のセットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

StreamReader(Stream, Encoding, Boolean)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定した文字エンコードとバイトオーダー マーク検出オプションを使用して、指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

public:
 StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks);
public StreamReader(System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks);
public StreamReader(System.IO.Stream stream, System.Text.Encoding? encoding, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean)

パラメーター

stream
Stream

読み取るストリーム。

encoding
Encoding

使用する文字エンコード。

detectEncodingFromByteOrderMarks
Boolean

ファイルの先頭でバイト順マークを検索するかどうかを示します。

例外

stream は読み取りをサポートしていません。

stream または encodingnull

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

private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

注釈

このコンストラクターは、encoding パラメーターで指定されたエンコード、stream パラメーターを使用したBaseStream プロパティ、および内部バッファー サイズを 1024 バイトに初期化します。

detectEncodingFromByteOrderMarks パラメーターは、ストリームの最初の 4 バイトを調べることでエンコードを検出します。 ファイルが適切なバイトオーダー マークで始まる場合、UTF-8、リトル エンディアン UTF-16、ビッグ エンディアン UTF-16、リトル エンディアン UTF-32、ビッグ エンディアン UTF-32 テキストが自動的に認識されます。 それ以外の場合は、ユーザー指定のエンコードが使用されます。 詳細については、 Encoding.GetPreamble メソッドを参照してください。

StreamReader オブジェクトは、StreamReader.Disposeが呼び出されたときに、指定されたStream オブジェクトに対してDispose()を呼び出します。

注意事項

特定のカルチャ設定で文字のセットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

StreamReader(String, Encoding, Boolean)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定した文字エンコードとバイトオーダー マーク検出オプションを使用して、指定したファイル名の StreamReader クラスの新しいインスタンスを初期化します。

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks);
public StreamReader(string path, System.Text.Encoding? encoding, bool detectEncodingFromByteOrderMarks);
public StreamReader(string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : string * System.Text.Encoding * bool -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean)

パラメーター

path
String

読み取る完全なファイル パス。

encoding
Encoding

使用する文字エンコード。

detectEncodingFromByteOrderMarks
Boolean

ファイルの先頭でバイト順マークを検索するかどうかを示します。

例外

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

path または encodingnull

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

マップされていないドライブ上など、指定されたパスが無効です。

path には、ファイル名、ディレクトリ名、またはボリューム ラベルの構文が正しくないか無効です。

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

private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

注釈

このコンストラクターは、 encoding パラメーターで指定されたエンコードを初期化し、内部バッファー サイズを 1024 バイトに初期化します。

detectEncodingFromByteOrderMarks パラメーターは、ストリームの最初の 4 バイトを調べることでエンコードを検出します。 ファイルが適切なバイトオーダー マークで始まる場合、UTF-8、リトル エンディアン UTF-16、ビッグ エンディアン UTF-16、リトル エンディアン UTF-32、ビッグ エンディアン UTF-32 テキストが自動的に認識されます。 それ以外の場合は、ユーザー指定のエンコードが使用されます。 詳細については、 Encoding.GetPreamble メソッドを参照してください。

path パラメーターには、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

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

注意事項

特定のカルチャ設定で文字のセットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

StreamReader(String, FileStreamOptions)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

既定のエンコードを使用して、指定したファイル パスの StreamReader クラスの新しいインスタンスを初期化し、ファイルの先頭にあるバイトオーダー マークの検出を有効にし、指定した FileStreamOptions オブジェクトで構成します。

public:
 StreamReader(System::String ^ path, System::IO::FileStreamOptions ^ options);
public StreamReader(string path, System.IO.FileStreamOptions options);
new System.IO.StreamReader : string * System.IO.FileStreamOptions -> System.IO.StreamReader
Public Sub New (path As String, options As FileStreamOptions)

パラメーター

path
String

読み取る完全なファイル パス。

options
FileStreamOptions

基になる FileStreamの構成オプションを指定するオブジェクト。

例外

path は読み取り不可能です。

-又は-

      <code data-dev-comment-type="paramref">path</code> is an empty string ("").

path または optionsnull

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

マップされていないドライブ上など、指定されたパスが無効です。

path には、ファイル名、ディレクトリ名、またはボリューム ラベルの構文が正しくないか無効です。

こちらもご覧ください

適用対象

StreamReader(String, Boolean)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定したバイト オーダー マーク検出オプションを使用して、指定したファイル名の StreamReader クラスの新しいインスタンスを初期化します。

public:
 StreamReader(System::String ^ path, bool detectEncodingFromByteOrderMarks);
public StreamReader(string path, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : string * bool -> System.IO.StreamReader
Public Sub New (path As String, detectEncodingFromByteOrderMarks As Boolean)

パラメーター

path
String

読み取る完全なファイル パス。

detectEncodingFromByteOrderMarks
Boolean

ファイルの先頭でバイト順マークを検索するかどうかを示します。

例外

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

pathnullです。

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

マップされていないドライブ上など、指定されたパスが無効です。

path には、ファイル名、ディレクトリ名、またはボリューム ラベルの構文が正しくないか無効です。

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

private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

注釈

このコンストラクターは、UTF8Encodingへのエンコード、stream パラメーターを使用した BaseStream プロパティ、および内部バッファー サイズを 1024 バイトに初期化します。

path パラメーターには、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

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

detectEncodingFromByteOrderMarks パラメーターは、ストリームの最初の 4 バイトを調べることでエンコードを検出します。 ファイルが適切なバイトオーダー マークで始まる場合、UTF-8、リトル エンディアン UTF-16、ビッグ エンディアン UTF-16、リトル エンディアン UTF-32、ビッグ エンディアン UTF-32 テキストが自動的に認識されます。 それ以外の場合は、 UTF8Encoding が使用されます。 詳細については、 Encoding.GetPreamble メソッドを参照してください。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

StreamReader(Stream, Encoding)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定した文字エンコードを使用して、指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

public:
 StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding);
public StreamReader(System.IO.Stream stream, System.Text.Encoding encoding);
public StreamReader(System.IO.Stream stream, System.Text.Encoding? encoding);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding)

パラメーター

stream
Stream

読み取るストリーム。

encoding
Encoding

使用する文字エンコード。

例外

stream は読み取りをサポートしていません。

stream または encodingnull

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

private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

注釈

文字エンコードは encoding パラメーターによって設定され、バッファー サイズは 1024 バイトに設定されます。 StreamReader オブジェクトは、ストリームの最初の 4 バイトを調べることでエンコードの検出を試みます。 ファイルが適切なバイトオーダー マークで始まる場合、UTF-8、リトル エンディアン UTF-16、ビッグ エンディアン UTF-16、リトル エンディアン UTF-32、ビッグ エンディアン UTF-32 テキストが自動的に認識されます。 それ以外の場合は、ユーザー指定のエンコードが使用されます。 詳細については、 Encoding.GetPreamble メソッドを参照してください。

StreamReader オブジェクトは、StreamReader.Disposeが呼び出されたときに、指定されたStream オブジェクトに対してDispose()を呼び出します。

注意事項

特定のカルチャ設定で文字のセットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

StreamReader(Stream, Boolean)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定したバイトオーダー マーク検出オプションを使用して、指定したストリームの StreamReader クラスの新しいインスタンスを初期化します。

public:
 StreamReader(System::IO::Stream ^ stream, bool detectEncodingFromByteOrderMarks);
public StreamReader(System.IO.Stream stream, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : System.IO.Stream * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, detectEncodingFromByteOrderMarks As Boolean)

パラメーター

stream
Stream

読み取るストリーム。

detectEncodingFromByteOrderMarks
Boolean

ファイルの先頭でバイト順マークを検索するかどうかを示します。

例外

stream は読み取りをサポートしていません。

streamnullです。

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

private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

注釈

このコンストラクターは、UTF8Encodingへのエンコード、stream パラメーターを使用した BaseStream プロパティ、および内部バッファー サイズを 1024 バイトに初期化します。

detectEncodingFromByteOrderMarks パラメーターは、ストリームの最初の 4 バイトを調べることでエンコードを検出します。 ファイルが適切なバイトオーダー マークで始まる場合、UTF-8、リトル エンディアン UTF-16、ビッグ エンディアン UTF-16、リトル エンディアン UTF-32、ビッグ エンディアン UTF-32 テキストが自動的に認識されます。 詳細については、 Encoding.GetPreamble メソッドを参照してください。

StreamReader オブジェクトは、StreamReader.Disposeが呼び出されたときに、指定されたStream オブジェクトに対してDispose()を呼び出します。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

StreamReader(String)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

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

public:
 StreamReader(System::String ^ path);
public StreamReader(string path);
new System.IO.StreamReader : string -> System.IO.StreamReader
Public Sub New (path As String)

パラメーター

path
String

読み取る完全なファイル パス。

例外

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

pathnullです。

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

マップされていないドライブ上など、指定されたパスが無効です。

path には、ファイル名、ディレクトリ名、またはボリューム ラベルの構文が正しくないか無効です。

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

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());
        }
    }
}
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

注釈

完全なファイル パスは、 path パラメーターによって指定されます。 このコンストラクターは、エンコードを UTF8Encoding に初期化し、バッファー サイズを 1024 バイトに初期化します。

path パラメーターには、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

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

注意事項

特定のカルチャ設定で文字のセットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

StreamReader(String, Encoding)

ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs
ソース:
StreamReader.cs

指定した文字エンコードを使用して、指定したファイル名の StreamReader クラスの新しいインスタンスを初期化します。

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding);
public StreamReader(string path, System.Text.Encoding? encoding);
public StreamReader(string path, System.Text.Encoding encoding);
new System.IO.StreamReader : string * System.Text.Encoding -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding)

パラメーター

path
String

読み取る完全なファイル パス。

encoding
Encoding

使用する文字エンコード。

例外

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

path または encodingnull

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

マップされていないドライブ上など、指定されたパスが無効です。

path には、ファイル名、ディレクトリ名、またはボリューム ラベルの構文が正しくないか無効です。

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

private void getNewStreamReader()
{
    //Get a new StreamReader in ASCII format from a
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file
    StreamReader srAsciiFromFile =
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //file with byte order mark detection = false
    StreamReader srFromFileFalse =
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile =
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'file with byte order mark detection = false
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false and a buffer
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

注釈

このコンストラクターは、 encoding パラメーターで指定されたエンコードを初期化し、内部バッファー サイズを 1024 バイトに初期化します。 StreamReader オブジェクトは、ストリームの最初の 4 バイトを調べることでエンコードの検出を試みます。 ファイルが適切なバイトオーダー マークで始まる場合、UTF-8、リトル エンディアン UTF-16、ビッグ エンディアン UTF-16、リトル エンディアン UTF-32、ビッグ エンディアン UTF-32 テキストが自動的に認識されます。 それ以外の場合は、ユーザー指定のエンコードが使用されます。 詳細については、 Encoding.GetPreamble メソッドを参照してください。

path パラメーターには、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

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

注意事項

特定のカルチャ設定で文字のセットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象