StreamReader StreamReader StreamReader StreamReader Constructors

定義

多載

StreamReader(Stream) StreamReader(Stream) StreamReader(Stream)

為指定的資料流,初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream.

StreamReader(String) StreamReader(String) StreamReader(String) StreamReader(String)

為指定的檔名初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name.

StreamReader(Stream, Boolean) StreamReader(Stream, Boolean) StreamReader(Stream, Boolean)

使用指定的位元組順序標記偵測選項,針對指定的資料流初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream, with the specified byte order mark detection option.

StreamReader(Stream, Encoding) StreamReader(Stream, Encoding) StreamReader(Stream, Encoding)

使用指定的字元編碼方式,針對指定的資料流初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding.

StreamReader(String, Boolean) StreamReader(String, Boolean) StreamReader(String, Boolean) StreamReader(String, Boolean)

使用指定的位元組順序標記偵測選項,為指定的檔案名稱初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name, with the specified byte order mark detection option.

StreamReader(String, Encoding) StreamReader(String, Encoding) StreamReader(String, Encoding)

使用指定的字元編碼方式,針對指定的檔案名稱初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding.

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

使用指定的字元編碼方式和位元組順序標記偵測選項,為指定的資料流初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.

StreamReader(String, Encoding, Boolean) StreamReader(String, Encoding, Boolean) StreamReader(String, Encoding, Boolean)

使用指定的字元編碼方式和位元組順序標記偵測選項,為指定的檔案名稱初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option.

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

使用指定的字元編碼方式、位元組順序標記偵測選項和緩衝區大小,為指定的資料流初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding, byte order mark detection option, and buffer size.

StreamReader(String, Encoding, Boolean, Int32) StreamReader(String, Encoding, Boolean, Int32) StreamReader(String, Encoding, Boolean, Int32)

使用指定的字元編碼方式、位元組順序標記偵測選項和緩衝區大小,為指定的檔案名稱初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding, byte order mark detection option, and buffer size.

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

根據指定的字元編碼方式、位元順序標記偵測選項和緩衝區大小,為指定的資料流初始化 StreamReader 類別的新執行個體,並選擇性讓資料流保持開啟。Initializes a new instance of the StreamReader class for the specified stream based on the specified character encoding, byte order mark detection option, and buffer size, and optionally leaves the stream open.

StreamReader(Stream) StreamReader(Stream) StreamReader(Stream)

為指定的資料流,初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream.

public:
 StreamReader(System::IO::Stream ^ stream);
public StreamReader (System.IO.Stream stream);
new System.IO.StreamReader : System.IO.Stream -> System.IO.StreamReader

參數

stream
Stream Stream Stream Stream

要讀取的資料流。The stream to be read.

例外狀況

stream 不支援讀取。stream does not support reading.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

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 );
   }
}
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方式初始化BaseStream為、使用stream參數的屬性,以及將內部緩衝區大小設為1024個位元組。This constructor initializes the encoding to UTF8Encoding, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes.

StreamReader呼叫時Stream Dispose() ,物件StreamReader.Dispose會在所提供的物件上呼叫。The StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.

警告

當您使用特定的文化特性設定來編譯一組字元,並使用不同的文化特性設定來抓取那些相同的字元時,可能不會可解譯這些字元,而且可能會擲回例外狀況。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

StreamReader(String) StreamReader(String) StreamReader(String) StreamReader(String)

為指定的檔名初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name.

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 String String String

要讀取的完整檔案路徑。The complete file path to be read.

例外狀況

path 為空字串 ("")。path is an empty string ("").

指定的路徑無效,例如位於未對應的磁碟機上。The specified path is invalid, such as being on an unmapped drive.

path 包含檔案名稱、目錄名稱或磁碟標籤的不正確或無效語法。path includes an incorrect or invalid syntax for file name, directory name, or volume label.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

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 );
   }
}
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參數指定。The complete file path is specified by the path parameter. 此函式會將編碼UTF8Encoding為,並將緩衝區大小初始化為1024個位元組。This constructor initializes the encoding to UTF8Encoding and the buffer size to 1024 bytes.

path參數可以是一個檔案名,包括通用命名慣例(UNC)共用上的檔案。The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

path參數不一定要是儲存在磁片上的檔案,它可以是系統中支援使用資料流程存取的任何部分。The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

警告

當您使用特定的文化特性設定來編譯一組字元,並使用不同的文化特性設定來抓取那些相同的字元時,可能不會可解譯這些字元,而且可能會擲回例外狀況。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

StreamReader(Stream, Boolean) StreamReader(Stream, Boolean) StreamReader(Stream, Boolean)

使用指定的位元組順序標記偵測選項,針對指定的資料流初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream, with the specified byte order mark detection option.

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

參數

stream
Stream Stream Stream Stream

要讀取的資料流。The stream to be read.

detectEncodingFromByteOrderMarks
Boolean Boolean Boolean Boolean

表示是否在檔案開頭尋找位元組順序標記。Indicates whether to look for byte order marks at the beginning of the file.

例外狀況

stream 不支援讀取。stream does not support reading.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}


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方式初始化BaseStream為、使用stream參數的屬性,以及將內部緩衝區大小設為1024個位元組。This constructor initializes the encoding to UTF8Encoding, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes.

detectEncodingFromByteOrderMarks參數會藉由查看資料流程的前四個位元組來偵測編碼方式。The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. 如果檔案是以適當的位元組順序標記開頭,它就會自動辨識 UTF-8、不限位元組的 unicode、位元組由大到32小的 Unicode 和位元組後序的 UTF-32 文字。It automatically recognizes UTF-8, little-endian Unicode, big-endian Unicode, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. 如需詳細資訊,請參閱 Encoding.GetPreamble 方法。See the Encoding.GetPreamble method for more information.

StreamReader呼叫時Stream Dispose() ,物件StreamReader.Dispose會在所提供的物件上呼叫。The StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

StreamReader(Stream, Encoding) StreamReader(Stream, Encoding) StreamReader(Stream, Encoding)

使用指定的字元編碼方式,針對指定的資料流初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream, with the specified character 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

參數

stream
Stream Stream Stream Stream

要讀取的資料流。The stream to be read.

encoding
Encoding Encoding Encoding Encoding

要使用的字元編碼。The character encoding to use.

例外狀況

stream 不支援讀取。stream does not support reading.

streamencodingnullstream or encoding is null.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}


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個位元組。The character encoding is set by the encoding parameter, and the buffer size is set to 1024 bytes. StreamReader物件會藉由查看資料流程的前四個位元組,嘗試偵測編碼。The StreamReader object attempts to detect the encoding by looking at the first four bytes of the stream. 如果檔案是以適當的位元組順序標記開頭,它就會自動辨識 UTF-8、不限位元組的 unicode、位元組由大到32小的 Unicode 和位元組後序的 UTF-32 文字。It automatically recognizes UTF-8, little-endian Unicode, big-endian Unicode, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. 否則,會使用使用者提供的編碼方式。Otherwise, the user-provided encoding is used. 如需詳細資訊,請參閱 Encoding.GetPreamble 方法。See the Encoding.GetPreamble method for more information.

StreamReader呼叫時Stream Dispose() ,物件StreamReader.Dispose會在所提供的物件上呼叫。The StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.

警告

當您使用特定的文化特性設定來編譯一組字元,並使用不同的文化特性設定來抓取那些相同的字元時,可能不會可解譯這些字元,而且可能會擲回例外狀況。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

StreamReader(String, Boolean) StreamReader(String, Boolean) StreamReader(String, Boolean) StreamReader(String, Boolean)

使用指定的位元組順序標記偵測選項,為指定的檔案名稱初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name, with the specified byte order mark detection option.

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 String String String

要讀取的完整檔案路徑。The complete file path to be read.

detectEncodingFromByteOrderMarks
Boolean Boolean Boolean Boolean

表示是否在檔案開頭尋找位元組順序標記。Indicates whether to look for byte order marks at the beginning of the file.

例外狀況

path 為空字串 ("")。path is an empty string ("").

指定的路徑無效,例如位於未對應的磁碟機上。The specified path is invalid, such as being on an unmapped drive.

path 包含檔案名稱、目錄名稱或磁碟標籤的不正確或無效語法。path includes an incorrect or invalid syntax for file name, directory name, or volume label.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}


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方式初始化BaseStream為、使用stream參數的屬性,以及將內部緩衝區大小設為1024個位元組。This constructor initializes the encoding to UTF8Encoding, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes.

path參數可以是一個檔案名,包括通用命名慣例(UNC)共用上的檔案。The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

path參數不一定要是儲存在磁片上的檔案,它可以是系統中支援使用資料流程存取的任何部分。The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

detectEncodingFromByteOrderMarks參數會藉由查看資料流程的前四個位元組來偵測編碼方式。The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. 如果檔案是以適當的位元組順序標記開頭,它就會自動辨識 UTF-8、不限位元組的 unicode、位元組由大到32小的 Unicode 和位元組後序的 UTF-32 文字。It automatically recognizes UTF-8, little-endian Unicode, big-endian Unicode, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. 否則, UTF8Encoding就會使用。Otherwise, the UTF8Encoding is used. 如需詳細資訊,請參閱 Encoding.GetPreamble 方法。See the Encoding.GetPreamble method for more information.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

StreamReader(String, Encoding) StreamReader(String, Encoding) StreamReader(String, Encoding)

使用指定的字元編碼方式,針對指定的檔案名稱初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding.

public:
 StreamReader(System::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

參數

path
String String String String

要讀取的完整檔案路徑。The complete file path to be read.

encoding
Encoding Encoding Encoding Encoding

要使用的字元編碼。The character encoding to use.

例外狀況

path 為空字串 ("")。path is an empty string ("").

pathencodingnullpath or encoding is null.

指定的路徑無效,例如位於未對應的磁碟機上。The specified path is invalid, such as being on an unmapped drive.

path 包含檔案名稱、目錄名稱或磁碟標籤的不正確或無效語法。path includes an incorrect or invalid syntax for file name, directory name, or volume label.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}


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個位元組。This constructor initializes the encoding as specified by the encoding parameter, and the internal buffer size to 1024 bytes. StreamReader物件會藉由查看資料流程的前四個位元組,嘗試偵測編碼。The StreamReader object attempts to detect the encoding by looking at the first four bytes of the stream. 如果檔案是以適當的位元組順序標記開頭,它就會自動辨識 UTF-8、不限位元組的 unicode、位元組由大到32小的 Unicode 和位元組後序的 UTF-32 文字。It automatically recognizes UTF-8, little-endian Unicode, big-endian Unicode, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. 否則,會使用使用者提供的編碼方式。Otherwise, the user-provided encoding is used. 如需詳細資訊,請參閱 Encoding.GetPreamble 方法。See the Encoding.GetPreamble method for more information.

path參數可以是一個檔案名,包括通用命名慣例(UNC)共用上的檔案。The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

path參數不一定要是儲存在磁片上的檔案,它可以是系統中支援使用資料流程存取的任何部分。The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

警告

當您使用特定的文化特性設定來編譯一組字元,並使用不同的文化特性設定來抓取那些相同的字元時,可能不會可解譯這些字元,而且可能會擲回例外狀況。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

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

使用指定的字元編碼方式和位元組順序標記偵測選項,為指定的資料流初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.

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

參數

stream
Stream Stream Stream Stream

要讀取的資料流。The stream to be read.

encoding
Encoding Encoding Encoding Encoding

要使用的字元編碼。The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean Boolean Boolean Boolean

表示是否在檔案開頭尋找位元組順序標記。Indicates whether to look for byte order marks at the beginning of the file.

例外狀況

stream 不支援讀取。stream does not support reading.

streamencodingnullstream or encoding is null.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}


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參數所指定的編碼、使用BaseStream stream參數的屬性,以及將內部緩衝區大小設為1024個位元組。This constructor initializes the encoding as specified by the encoding parameter, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes.

detectEncodingFromByteOrderMarks參數會藉由查看資料流程的前四個位元組來偵測編碼方式。The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. 如果檔案是以適當的位元組順序標記開頭,它就會自動辨識 UTF-8、不限位元組的 unicode、位元組由大到32小的 Unicode 和位元組後序的 UTF-32 文字。It automatically recognizes UTF-8, little-endian Unicode, big-endian Unicode, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. 否則,會使用使用者提供的編碼方式。Otherwise, the user-provided encoding is used. 如需詳細資訊,請參閱 Encoding.GetPreamble 方法。See the Encoding.GetPreamble method for more information.

StreamReader呼叫時Stream Dispose() ,物件StreamReader.Dispose會在所提供的物件上呼叫。The StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.

警告

當您使用特定的文化特性設定來編譯一組字元,並使用不同的文化特性設定來抓取那些相同的字元時,可能不會可解譯這些字元,而且可能會擲回例外狀況。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

StreamReader(String, Encoding, Boolean) StreamReader(String, Encoding, Boolean) StreamReader(String, Encoding, Boolean)

使用指定的字元編碼方式和位元組順序標記偵測選項,為指定的檔案名稱初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option.

public:
 StreamReader(System::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

參數

path
String String String String

要讀取的完整檔案路徑。The complete file path to be read.

encoding
Encoding Encoding Encoding Encoding

要使用的字元編碼。The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean Boolean Boolean Boolean

表示是否在檔案開頭尋找位元組順序標記。Indicates whether to look for byte order marks at the beginning of the file.

例外狀況

path 為空字串 ("")。path is an empty string ("").

pathencodingnullpath or encoding is null.

指定的路徑無效,例如位於未對應的磁碟機上。The specified path is invalid, such as being on an unmapped drive.

path 包含檔案名稱、目錄名稱或磁碟標籤的不正確或無效語法。path includes an incorrect or invalid syntax for file name, directory name, or volume label.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}


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個位元組。This constructor initializes the encoding as specified by the encoding parameter, and the internal buffer size to 1024 bytes.

detectEncodingFromByteOrderMarks參數會藉由查看資料流程的前四個位元組來偵測編碼方式。The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. 如果檔案是以適當的位元組順序標記開頭,它就會自動辨識 UTF-8、不限位元組的 unicode、位元組由大到32小的 Unicode 和位元組後序的 UTF-32 文字。It automatically recognizes UTF-8, little-endian Unicode, big-endian Unicode, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. 否則,會使用使用者提供的編碼方式。Otherwise, the user-provided encoding is used. 如需詳細資訊,請參閱 Encoding.GetPreamble 方法。See the Encoding.GetPreamble method for more information.

path參數可以是一個檔案名,包括通用命名慣例(UNC)共用上的檔案。The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

path參數不一定要是儲存在磁片上的檔案,它可以是系統中支援使用資料流程存取的任何部分。The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

警告

當您使用特定的文化特性設定來編譯一組字元,並使用不同的文化特性設定來抓取那些相同的字元時,可能不會可解譯這些字元,而且可能會擲回例外狀況。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

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

使用指定的字元編碼方式、位元組順序標記偵測選項和緩衝區大小,為指定的資料流初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding, byte order mark detection option, and buffer size.

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

參數

stream
Stream Stream Stream Stream

要讀取的資料流。The stream to be read.

encoding
Encoding Encoding Encoding Encoding

要使用的字元編碼。The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean Boolean Boolean Boolean

表示是否在檔案開頭尋找位元組順序標記。Indicates whether to look for byte order marks at the beginning of the file.

bufferSize
Int32 Int32 Int32 Int32

最小緩衝區大小。The minimum buffer size.

例外狀況

資料流不支援讀取。The stream does not support reading.

streamencodingnullstream or encoding is null.

bufferSize 小於或等於零。bufferSize is less than or equal to zero.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}


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

備註

緩衝區大小(以16位字元數為單位)是由bufferSize參數所設定。The buffer size, in number of 16-bit characters, is set by the bufferSize parameter. 如果bufferSize小於允許的大小下限(128個字元),則會使用允許的最小大小。If bufferSize is less than the minimum allowable size (128 characters), the minimum allowable size is used.

這個函數可讓您在第一次從StreamReader物件讀取時變更編碼。This constructor allows you to change the encoding the first time you read from the StreamReader object. detectEncodingFromByteOrderMarks參數會藉由查看資料流程的前四個位元組來偵測編碼方式。The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. 如果檔案是以適當的位元組順序標記開頭,它就會自動辨識 UTF-8、不限位元組的 unicode、位元組由大到32小的 Unicode 和位元組後序的 UTF-32 文字。It automatically recognizes UTF-8, little-endian Unicode, big-endian Unicode, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. 否則,會使用使用者提供的編碼方式。Otherwise, the user-provided encoding is used. 如需詳細資訊,請參閱 Encoding.GetPreamble 方法。See the Encoding.GetPreamble method for more information.

StreamReader呼叫時Stream Dispose() ,物件StreamReader.Dispose會在所提供的物件上呼叫。The StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.

注意

從讀取時Stream,使用與資料流程內部緩衝區相同大小的緩衝區會更有效率。When reading from a Stream, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.

警告

當您使用特定的文化特性設定來編譯一組字元,並使用不同的文化特性設定來抓取那些相同的字元時,可能不會可解譯這些字元,而且可能會擲回例外狀況。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

StreamReader(String, Encoding, Boolean, Int32) StreamReader(String, Encoding, Boolean, Int32) StreamReader(String, Encoding, Boolean, Int32)

使用指定的字元編碼方式、位元組順序標記偵測選項和緩衝區大小,為指定的檔案名稱初始化 StreamReader 類別的新執行個體。Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding, byte order mark detection option, and buffer size.

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);
new System.IO.StreamReader : string * System.Text.Encoding * bool * int -> System.IO.StreamReader

參數

path
String String String String

要讀取的完整檔案路徑。The complete file path to be read.

encoding
Encoding Encoding Encoding Encoding

要使用的字元編碼。The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean Boolean Boolean Boolean

表示是否在檔案開頭尋找位元組順序標記。Indicates whether to look for byte order marks at the beginning of the file.

bufferSize
Int32 Int32 Int32 Int32

最小緩衝區大小,使用 16 位元字元的數字。The minimum buffer size, in number of 16-bit characters.

例外狀況

path 為空字串 ("")。path is an empty string ("").

pathencodingnullpath or encoding is null.

指定的路徑無效,例如位於未對應的磁碟機上。The specified path is invalid, such as being on an unmapped drive.

path 包含檔案名稱、目錄名稱或磁碟標籤的不正確或無效語法。path includes an incorrect or invalid syntax for file name, directory name, or volume label.

buffersize 小於或等於零。buffersize is less than or equal to zero.

範例

下列程式碼範例將示範StreamReader這個函數。The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew StreamReader( 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}


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參數所指定的編碼方式。This constructor initializes the encoding as specified by the encoding parameter.

這個函數可讓您在第一次從StreamReader物件讀取時變更編碼。This constructor allows you to change the encoding the first time you read from the StreamReader object. detectEncodingFromByteOrderMarks參數會藉由查看資料流程的前四個位元組來偵測編碼方式。The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. 如果檔案是以適當的位元組順序標記開頭,它就會自動辨識 UTF-8、不限位元組的 unicode、位元組由大到32小的 Unicode 和位元組後序的 UTF-32 文字。It automatically recognizes UTF-8, little-endian Unicode, big-endian Unicode, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. 否則,會使用使用者提供的編碼方式。Otherwise, the user-provided encoding is used. 如需詳細資訊,請參閱 Encoding.GetPreamble 方法。See the Encoding.GetPreamble method for more information.

緩衝區大小(以16位字元數為單位)是由bufferSize參數所設定。The buffer size, in number of 16-bit characters, is set by the bufferSize parameter. 如果bufferSize小於允許的大小下限(128個字元),則會使用允許的最小大小。If bufferSize is less than the minimum allowable size (128 characters), the minimum allowable size is used.

path參數可以是一個檔案名,包括通用命名慣例(UNC)共用上的檔案。The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

path參數不一定要是儲存在磁片上的檔案,它可以是系統中支援使用資料流程存取的任何部分。The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

警告

當您使用特定的文化特性設定來編譯一組字元,並使用不同的文化特性設定來抓取那些相同的字元時,可能不會可解譯這些字元,而且可能會擲回例外狀況。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

另請參閱

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

根據指定的字元編碼方式、位元順序標記偵測選項和緩衝區大小,為指定的資料流初始化 StreamReader 類別的新執行個體,並選擇性讓資料流保持開啟。Initializes a new instance of the StreamReader class for the specified stream based on the specified character encoding, byte order mark detection option, and buffer size, and optionally leaves the stream open.

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 = null, 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

參數

stream
Stream Stream Stream Stream

要讀取的資料流。The stream to read.

encoding
Encoding Encoding Encoding Encoding

要使用的字元編碼。The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean Boolean Boolean Boolean

true 表示在檔案開頭尋找位元順序標記,否則為 falsetrue to look for byte order marks at the beginning of the file; otherwise, false.

bufferSize
Int32 Int32 Int32 Int32

最小緩衝區大小。The minimum buffer size.

leaveOpen
Boolean Boolean Boolean Boolean

true 表示在處置 StreamReader 物件之後,將資料流保持開啟;否則為 falsetrue to leave the stream open after the StreamReader object is disposed; otherwise, false.

備註

除非您leaveOpen將參數設為trueStreamReader.Dispose StreamReader否則當呼叫Dispose()時,物件Stream會在所提供的物件上呼叫。Unless you set the leaveOpen parameter to true, the StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.

緩衝區大小(以16位字元數為單位)是由bufferSize參數所設定。The buffer size, in number of 16-bit characters, is set by the bufferSize parameter. 如果bufferSize小於允許的大小下限(128個字元),則會使用允許的最小大小。If bufferSize is less than the minimum allowable size (128 characters), the minimum allowable size is used.

這個函數可讓您在第一次從StreamReader物件讀取時變更編碼。This constructor enables you to change the encoding the first time you read from the StreamReader object. 如果參數為, true則此函式會藉由查看資料流程的前四個位元組來偵測編碼。 detectEncodingFromByteOrderMarksIf the detectEncodingFromByteOrderMarks parameter is true, the constructor detects the encoding by looking at the first four bytes of the stream. 如果檔案是以適當的位元組順序標記開頭,它就會自動辨識 UTF-8、不限位元組的 unicode、位元組由大到32小的 Unicode 和位元組後序的 UTF-32 文字。It automatically recognizes UTF-8, little-endian Unicode, big-endian Unicode, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. 否則,會使用使用者提供的編碼方式。Otherwise, the user-provided encoding is used. 如需詳細資訊,請參閱 Encoding.GetPreamble 方法。See the Encoding.GetPreamble method for more information.

注意

從讀取時Stream,使用與資料流程內部緩衝區相同大小的緩衝區會更有效率。When reading from a Stream, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.

警告

當您使用特定的文化特性設定來編譯一組字元,並使用不同的文化特性設定來抓取那些相同的字元時,可能無法正確地解讀字元,而且可能會擲回例外狀況。When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpreted correctly, and could cause an exception to be thrown.

適用於