StreamReader 构造函数

定义

为指定的流初始化 StreamReader 类的新实例。Initializes a new instance of the StreamReader class for the specified stream.

重载

StreamReader(Stream)

为指定的流初始化 StreamReader 类的新实例。Initializes a new instance of the StreamReader class for the specified stream.

StreamReader(String)

为指定的文件名初始化 StreamReader 类的新实例。Initializes a new instance of the StreamReader class for the specified file name.

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 类的一个新实例。Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding.

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 类的一个新实例。Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding.

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 类的新实例,带有指定的字符编码和字节顺序标记检测选项。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 类的新实例,带有指定的字符编码、字节顺序标记检测选项和缓冲区大小。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 类的新实例,带有指定字符编码、字节顺序标记检测选项和缓冲区大小。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 类的新实例,带有指定的字符编码、字节顺序标记检测选项和缓冲区大小,有选择性的打开流。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 类的新实例。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
Public Sub New (stream As Stream)

参数

stream
Stream

要读取的流。The stream to be read.

例外

stream 不支持读取。stream does not support reading.

streamnullstream is null.

示例

下面的代码示例演示了此 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

注解

此构造函数将编码初始化为 UTF8EncodingBaseStream 使用参数的属性 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调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamReader.DisposeThe 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 类的新实例。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

要读取的完整文件路径。The complete file path to be read.

例外

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

pathnullpath is null.

找不到该文件。The file cannot be found.

指定的路径无效,例如位于未映射的驱动器上。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

注解

完整的文件路径是由参数指定的 pathThe 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 类的一个新实例。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
Public Sub New (stream As Stream, detectEncodingFromByteOrderMarks As Boolean)

参数

stream
Stream

要读取的流。The stream to be read.

detectEncodingFromByteOrderMarks
Boolean

指示是否在文件头查找字节顺序标记。Indicates whether to look for byte order marks at the beginning of the file.

例外

stream 不支持读取。stream does not support reading.

streamnullstream 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

注解

此构造函数将编码初始化为 UTF8EncodingBaseStream 使用参数的属性 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、大字节序 Unicode、小字节序 UTF-32 和大序位 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调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamReader.DisposeThe 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 类的一个新实例。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
Public Sub New (stream As Stream, encoding As Encoding)

参数

stream
Stream

要读取的流。The stream to be read.

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、大字节序 Unicode、小字节序 UTF-32 和大序位 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调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamReader.DisposeThe 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 类的新实例,带有指定的字节顺序标记检测选项。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

要读取的完整文件路径。The complete file path to be read.

detectEncodingFromByteOrderMarks
Boolean

指示是否在文件头查找字节顺序标记。Indicates whether to look for byte order marks at the beginning of the file.

例外

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

pathnullpath is null.

找不到该文件。The file cannot be found.

指定的路径无效,例如位于未映射的驱动器上。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

注解

此构造函数将编码初始化为 UTF8EncodingBaseStream 使用参数的属性 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、大字节序 Unicode、小字节序 UTF-32 和大序位 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 类的一个新实例。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
Public Sub New (path As String, encoding As Encoding)

参数

path
String

要读取的完整文件路径。The complete file path to be read.

encoding
Encoding

要使用的字符编码。The character encoding to use.

例外

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

pathencodingnullpath or encoding is null.

找不到该文件。The file cannot be found.

指定的路径无效,例如位于未映射的驱动器上。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、大字节序 Unicode、小字节序 UTF-32 和大序位 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 类的新实例,带有指定的字符编码和字节顺序标记检测选项。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
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean)

参数

stream
Stream

要读取的流。The stream to be read.

encoding
Encoding

要使用的字符编码。The character encoding to use.

detectEncodingFromByteOrderMarks
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

注解

此构造函数按参数指定的 encodingBaseStream 使用参数的属性 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、大字节序 Unicode、小字节序 UTF-32 和大序位 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调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamReader.DisposeThe 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 类的新实例,带有指定的字符编码和字节顺序标记检测选项。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
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean)

参数

path
String

要读取的完整文件路径。The complete file path to be read.

encoding
Encoding

要使用的字符编码。The character encoding to use.

detectEncodingFromByteOrderMarks
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 file cannot be found.

指定的路径无效,例如位于未映射的驱动器上。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、大字节序 Unicode、小字节序 UTF-32 和大序位 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 类的新实例,带有指定的字符编码、字节顺序标记检测选项和缓冲区大小。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
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)

参数

stream
Stream

要读取的流。The stream to be read.

encoding
Encoding

要使用的字符编码。The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean

指示是否在文件头查找字节顺序标记。Indicates whether to look for byte order marks at the beginning of the file.

bufferSize
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.

此构造函数允许您在第一次从对象读取时更改编码 StreamReaderThis 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、大字节序 Unicode、小字节序 UTF-32 和大序位 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调用时,对象对 Dispose() 提供的 Stream 对象调用 StreamReader.DisposeThe 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 类的新实例,带有指定字符编码、字节顺序标记检测选项和缓冲区大小。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
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)

参数

path
String

要读取的完整文件路径。The complete file path to be read.

encoding
Encoding

要使用的字符编码。The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean

指示是否在文件头查找字节顺序标记。Indicates whether to look for byte order marks at the beginning of the file.

bufferSize
Int32

最小缓冲区大小(以 16 位字符的数目为单位)。The minimum buffer size, in number of 16-bit characters.

例外

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

pathencodingnullpath or encoding is null.

找不到该文件。The file cannot be found.

指定的路径无效,例如位于未映射的驱动器上。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

注解

此构造函数按照参数指定的方式初始化编码 encodingThis constructor initializes the encoding as specified by the encoding parameter.

此构造函数允许您在第一次从对象读取时更改编码 StreamReaderThis 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、大字节序 Unicode、小字节序 UTF-32 和大序位 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 类的新实例,带有指定的字符编码、字节顺序标记检测选项和缓冲区大小,有选择性的打开流。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, 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

要读取的流。The stream to read.

encoding
Encoding

要使用的字符编码。The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean

如果要在文件开头查找字节顺序标记,则为true ;否则为 falsetrue to look for byte order marks at the beginning of the file; otherwise, false.

bufferSize
Int32

最小缓冲区大小。The minimum buffer size.

leaveOpen
Boolean

如果在释放 StreamReader 对象后保持流处于打开状态,则为 true;否则为 falsetrue to leave the stream open after the StreamReader object is disposed; otherwise, false.

注解

除非您设置leaveOpen参数true,则StreamReader对象调用Dispose()上提供Stream对象时StreamReader.Dispose调用。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.

此构造函数使你能够在第一次从对象读取时更改编码 StreamReaderThis constructor enables you to change the encoding the first time you read from the StreamReader object. 如果 detectEncodingFromByteOrderMarks 参数为 true ,则构造函数通过查看流的前四个字节来检测编码。If the detectEncodingFromByteOrderMarks parameter is true, the constructor detects the encoding by looking at the first four bytes of the stream. 如果文件以适当的字节顺序标记开头,则它会自动识别 UTF-8、小字节序 Unicode、大字节序 Unicode、小字节序 UTF-32 和大序位 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.

适用于