StreamReader 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为指定的流初始化 StreamReader 类的新实例。
重载
StreamReader(Stream)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
为指定的流初始化 StreamReader 类的新实例。
public:
StreamReader(System::IO::Stream ^ stream);
public StreamReader (System.IO.Stream stream);
new System.IO.StreamReader : System.IO.Stream -> System.IO.StreamReader
Public Sub New (stream As Stream)
参数
- stream
- Stream
要读取的流。
例外
stream
不支持读取。
stream
为 null
。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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 字节。
调用 时StreamReader.Dispose,对象StreamReaderDispose()对提供Stream的对象调用。
注意
编译具有特定区域性设置的一组字符并检索具有不同区域性设置的相同字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamReader(Stream, Encoding, Boolean, Int32, Boolean)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
为指定的流初始化 StreamReader 类的新实例,带有指定的字符编码、字节顺序标记检测选项和缓冲区大小,有选择性的打开流。
public:
StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen);
public StreamReader (System.IO.Stream stream, System.Text.Encoding? encoding = default, bool detectEncodingFromByteOrderMarks = true, int bufferSize = -1, bool leaveOpen = false);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool * int * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer, leaveOpen As Boolean)
Public Sub New (stream As Stream, Optional encoding As Encoding = Nothing, Optional detectEncodingFromByteOrderMarks As Boolean = true, Optional bufferSize As Integer = -1, Optional leaveOpen As Boolean = false)
参数
- stream
- Stream
要读取的流。
- encoding
- Encoding
要使用的字符编码。
- detectEncodingFromByteOrderMarks
- Boolean
如果要在文件开头查找字节顺序标记,则为true
;否则为 false
。
- bufferSize
- Int32
最小缓冲区大小。
- leaveOpen
- Boolean
如果在释放 StreamReader 对象后保持流处于打开状态,则为 true
;否则为 false
。
注解
除非您设置leaveOpen
参数true
,则StreamReader对象调用Dispose()上提供Stream对象时StreamReader.Dispose调用。
缓冲区大小(以 16 位字符数为单位)由 bufferSize
参数设置。 如果 bufferSize
小于允许的最小大小 (128 个字符) ,则使用允许的最小大小。
此构造函数使你能够在首次从 StreamReader 对象读取时更改编码。 detectEncodingFromByteOrderMarks
如果 参数为 true
,则构造函数通过查看流的前四个字节来检测编码。 如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。 否则,将使用用户提供的编码。 有关更多信息,请参阅 Encoding.GetPreamble 方法。
注意
从 Stream读取 时,使用与流内部缓冲区大小相同的缓冲区更高效。
注意
编译具有特定区域性设置的一组字符并检索具有不同区域性设置的相同字符时,这些字符可能无法正确解释,并可能导致引发异常。
适用于
StreamReader(String, Encoding, Boolean, FileStreamOptions)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
使用指定的字符编码、字节顺序标记检测选项并为指定的对象配置FileStreamOptions指定文件路径初始化 类的新实例StreamReader。
public:
StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, System::IO::FileStreamOptions ^ options);
public StreamReader (string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, System.IO.FileStreamOptions options);
new System.IO.StreamReader : string * System.Text.Encoding * bool * System.IO.FileStreamOptions -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, options As FileStreamOptions)
参数
- path
- String
要读取的完整文件路径。
- encoding
- Encoding
要使用的字符编码。
- detectEncodingFromByteOrderMarks
- Boolean
指示是否在文件头查找字节顺序标记。
- options
- FileStreamOptions
一个 对象,该对象指定基础 FileStream的配置选项。
例外
path
、encoding
或 options
为 null
。
找不到该文件。
指定的路径无效,例如位于未映射的驱动器上。
path
包含不正确或无效的文件名、目录名或卷标语法。
另请参阅
适用于
StreamReader(String, Encoding, Boolean, Int32)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
为指定的文件名初始化 StreamReader 类的新实例,带有指定字符编码、字节顺序标记检测选项和缓冲区大小。
public:
StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader (string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
new System.IO.StreamReader : string * System.Text.Encoding * bool * int -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)
参数
- path
- String
要读取的完整文件路径。
- encoding
- Encoding
要使用的字符编码。
- detectEncodingFromByteOrderMarks
- Boolean
指示是否在文件头查找字节顺序标记。
- bufferSize
- Int32
最小缓冲区大小(以 16 位字符的数目为单位)。
例外
path
为空字符串 ("")。
path
或 encoding
为 null
。
找不到该文件。
指定的路径无效,例如位于未映射的驱动器上。
path
包含不正确或无效的文件名、目录名或卷标语法。
buffersize
小于或等于零。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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
指定的编码。
此构造函数允许你在首次从 StreamReader 对象读取时更改编码。 参数 detectEncodingFromByteOrderMarks
通过查看流的前四个字节来检测编码。 如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。 否则,将使用用户提供的编码。 有关更多信息,请参阅 Encoding.GetPreamble 方法。
缓冲区大小(以 16 位字符数为单位)由 bufferSize
参数设置。 如果 bufferSize
小于允许的最小大小 (128 个字符) ,则使用允许的最小大小。
参数 path
可以是文件名,包括通用命名约定 (UNC) 共享上的文件。
参数 path
不一定是存储在磁盘上的文件;它可以是支持使用流进行访问的系统的任何部分。
注意
编译具有特定区域性设置的一组字符并检索具有不同区域性设置的相同字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamReader(Stream, Encoding, Boolean, Int32)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
为指定的流初始化 StreamReader 类的新实例,带有指定的字符编码、字节顺序标记检测选项和缓冲区大小。
public:
StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool * int -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)
参数
- stream
- Stream
要读取的流。
- encoding
- Encoding
要使用的字符编码。
- detectEncodingFromByteOrderMarks
- Boolean
指示是否在文件头查找字节顺序标记。
- bufferSize
- Int32
最小缓冲区大小。
例外
流不支持读取。
stream
或 encoding
为 null
。
bufferSize
小于或等于零。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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
参数设置。 如果 bufferSize
小于允许的最小大小 (128 个字符) ,则使用允许的最小大小。
此构造函数允许你在首次从 StreamReader 对象读取时更改编码。 参数 detectEncodingFromByteOrderMarks
通过查看流的前四个字节来检测编码。 如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。 否则,将使用用户提供的编码。 有关更多信息,请参阅 Encoding.GetPreamble 方法。
调用 时StreamReader.Dispose,对象StreamReaderDispose()对提供Stream的对象调用。
注意
从 Stream读取 时,使用与流内部缓冲区大小相同的缓冲区更高效。
注意
编译具有特定区域性设置的一组字符并检索具有不同区域性设置的相同字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamReader(Stream, Encoding, Boolean)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
为指定的流初始化 StreamReader 类的新实例,带有指定的字符编码和字节顺序标记检测选项。
public:
StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean)
参数
- stream
- Stream
要读取的流。
- encoding
- Encoding
要使用的字符编码。
- detectEncodingFromByteOrderMarks
- Boolean
指示是否在文件头查找字节顺序标记。
例外
stream
不支持读取。
stream
或 encoding
为 null
。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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 字节。
参数 detectEncodingFromByteOrderMarks
通过查看流的前四个字节来检测编码。 如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。 否则,将使用用户提供的编码。 有关更多信息,请参阅 Encoding.GetPreamble 方法。
调用 时StreamReader.Dispose,对象StreamReaderDispose()对提供Stream的对象调用。
注意
编译具有特定区域性设置的一组字符并检索具有不同区域性设置的相同字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamReader(String, Encoding, Boolean)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
为指定的文件名初始化 StreamReader 类的新实例,带有指定的字符编码和字节顺序标记检测选项。
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
要读取的完整文件路径。
- encoding
- Encoding
要使用的字符编码。
- detectEncodingFromByteOrderMarks
- Boolean
指示是否在文件头查找字节顺序标记。
例外
path
为空字符串 ("")。
path
或 encoding
为 null
。
找不到该文件。
指定的路径无效,例如位于未映射的驱动器上。
path
包含不正确或无效的文件名、目录名或卷标语法。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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 字节。
参数 detectEncodingFromByteOrderMarks
通过查看流的前四个字节来检测编码。 如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。 否则,将使用用户提供的编码。 有关更多信息,请参阅 Encoding.GetPreamble 方法。
参数 path
可以是文件名,包括通用命名约定 (UNC) 共享上的文件。
参数 path
不一定是存储在磁盘上的文件;它可以是支持使用流进行访问的系统的任何部分。
注意
编译具有特定区域性设置的一组字符并检索具有不同区域性设置的相同字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamReader(String, FileStreamOptions)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
使用默认编码为指定文件路径初始化 类的新实例 StreamReader ,启用文件开头的字节顺序标记检测,并使用指定的 FileStreamOptions 对象进行配置。
public:
StreamReader(System::String ^ path, System::IO::FileStreamOptions ^ options);
public StreamReader (string path, System.IO.FileStreamOptions options);
new System.IO.StreamReader : string * System.IO.FileStreamOptions -> System.IO.StreamReader
Public Sub New (path As String, options As FileStreamOptions)
参数
- path
- String
要读取的完整文件路径。
- options
- FileStreamOptions
一个 对象,该对象指定基础 FileStream的配置选项。
例外
stream
不可读。
- 或 -
<code data-dev-comment-type="paramref">path</code> is an empty string ("").
path
或 options
为 null
。
找不到该文件。
指定的路径无效,例如位于未映射的驱动器上。
path
包含不正确或无效的文件名、目录名或卷标语法。
另请参阅
适用于
StreamReader(String, Boolean)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
为指定的文件名初始化 StreamReader 类的新实例,带有指定的字节顺序标记检测选项。
public:
StreamReader(System::String ^ path, bool detectEncodingFromByteOrderMarks);
public StreamReader (string path, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : string * bool -> System.IO.StreamReader
Public Sub New (path As String, detectEncodingFromByteOrderMarks As Boolean)
参数
- path
- String
要读取的完整文件路径。
- detectEncodingFromByteOrderMarks
- Boolean
指示是否在文件头查找字节顺序标记。
例外
path
为空字符串 ("")。
path
为 null
。
找不到该文件。
指定的路径无效,例如位于未映射的驱动器上。
path
包含不正确或无效的文件名、目录名或卷标语法。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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 字节。
参数 path
可以是文件名,包括通用命名约定 (UNC) 共享上的文件。
参数 path
不一定是存储在磁盘上的文件;它可以是支持使用流进行访问的系统的任何部分。
参数 detectEncodingFromByteOrderMarks
通过查看流的前四个字节来检测编码。 如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。 否则, UTF8Encoding 使用 。 有关更多信息,请参阅 Encoding.GetPreamble 方法。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamReader(Stream, Encoding)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
用指定的字符编码为指定的流初始化 StreamReader 类的一个新实例。
public:
StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding)
参数
- stream
- Stream
要读取的流。
- encoding
- Encoding
要使用的字符编码。
例外
stream
不支持读取。
stream
或 encoding
为 null
。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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 字节。 对象 StreamReader 尝试通过查看流的前四个字节来检测编码。 如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。 否则,将使用用户提供的编码。 有关更多信息,请参阅 Encoding.GetPreamble 方法。
调用 时StreamReader.Dispose,对象StreamReaderDispose()对提供Stream的对象调用。
注意
编译具有特定区域性设置的一组字符并检索具有不同区域性设置的相同字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamReader(Stream, Boolean)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
用指定的字节顺序标记检测选项,为指定的流初始化 StreamReader 类的一个新实例。
public:
StreamReader(System::IO::Stream ^ stream, bool detectEncodingFromByteOrderMarks);
public StreamReader (System.IO.Stream stream, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : System.IO.Stream * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, detectEncodingFromByteOrderMarks As Boolean)
参数
- stream
- Stream
要读取的流。
- detectEncodingFromByteOrderMarks
- Boolean
指示是否在文件头查找字节顺序标记。
例外
stream
不支持读取。
stream
为 null
。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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 字节。
参数 detectEncodingFromByteOrderMarks
通过查看流的前四个字节来检测编码。 如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。 有关更多信息,请参阅 Encoding.GetPreamble 方法。
调用 时StreamReader.Dispose,对象StreamReaderDispose()对提供Stream的对象调用。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamReader(String)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
为指定的文件名初始化 StreamReader 类的新实例。
public:
StreamReader(System::String ^ path);
public StreamReader (string path);
new System.IO.StreamReader : string -> System.IO.StreamReader
Public Sub New (path As String)
参数
- path
- String
要读取的完整文件路径。
例外
path
为空字符串 ("")。
path
为 null
。
找不到该文件。
指定的路径无效,例如位于未映射的驱动器上。
path
包含不正确或无效的文件名、目录名或卷标语法。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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
参数指定。 此构造函数将编码初始化为 UTF8Encoding ,并将缓冲区大小初始化为 1024 字节。
参数 path
可以是文件名,包括通用命名约定 (UNC) 共享上的文件。
参数 path
不一定是存储在磁盘上的文件;它可以是支持使用流进行访问的系统的任何部分。
注意
编译具有特定区域性设置的一组字符并检索具有不同区域性设置的相同字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
StreamReader(String, Encoding)
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
- Source:
- StreamReader.cs
用指定的字符编码,为指定的文件名初始化 StreamReader 类的一个新实例。
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
要读取的完整文件路径。
- encoding
- Encoding
要使用的字符编码。
例外
path
为空字符串 ("")。
path
或 encoding
为 null
。
找不到该文件。
指定的路径无效,例如位于未映射的驱动器上。
path
包含不正确或无效的文件名、目录名或卷标语法。
示例
下面的代码示例演示了此 StreamReader 构造函数。
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 字节。 对象 StreamReader 尝试通过查看流的前四个字节来检测编码。 如果文件以适当的字节顺序标记开头,它会自动识别 UTF-8、little-endian UTF-16、big-endian UTF-16、little-endian UTF-32 和 big-endian UTF-32 文本。 否则,将使用用户提供的编码。 有关更多信息,请参阅 Encoding.GetPreamble 方法。
参数 path
可以是文件名,包括通用命名约定 (UNC) 共享上的文件。
参数 path
不一定是存储在磁盘上的文件;它可以是支持使用流进行访问的系统的任何部分。
注意
编译具有特定区域性设置的一组字符并检索具有不同区域性设置的相同字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。