C# System.IO.StreamReader

时间:2023-03-09 05:59:38
C#  System.IO.StreamReader

实现一个 TextReader,使其以一种特定的编码从字节流中读取字符。

 using System;
using System.IO; class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}

构造函数

StreamReader(Stream)

为指定的流初始化 StreamReader 类的新实例。

StreamReader(Stream, Boolean)

用指定的字节顺序标记检测选项,为指定的流初始化 StreamReader 类的一个新实例。

StreamReader(Stream, Encoding)

用指定的字符编码为指定的流初始化 StreamReader 类的一个新实例。

StreamReader(Stream, Encoding, Boolean)

为指定的流初始化 StreamReader 类的新实例,带有指定的字符编码和字节顺序标记检测选项。

StreamReader(Stream, Encoding, Boolean, Int32)

为指定的流初始化 StreamReader 类的新实例,带有指定的字符编码、字节顺序标记检测选项和缓冲区大小。

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

为指定的流初始化 StreamReader 类的新实例,带有指定的字符编码、字节顺序标记检测选项和缓冲区大小,有选择性的打开流。

StreamReader(String)

为指定的文件名初始化 StreamReader 类的新实例。

StreamReader(String, Boolean)

为指定的文件名初始化 StreamReader 类的新实例,带有指定的字节顺序标记检测选项。

StreamReader(String, Encoding)

用指定的字符编码,为指定的文件名初始化 StreamReader 类的一个新实例。

StreamReader(String, Encoding, Boolean)

为指定的文件名初始化 StreamReader 类的新实例,带有指定的字符编码和字节顺序标记检测选项。

StreamReader(String, Encoding, Boolean, Int32)

为指定的文件名初始化 StreamReader 类的新实例,带有指定字符编码、字节顺序标记检测选项和缓冲区大小。

字段

Null

空流周围的 StreamReader

属性

BaseStream

返回基础流。

CurrentEncoding

获取当前 StreamReader 对象正在使用的当前字符编码。

EndOfStream

获取一个值,该值表示当前的流位置是否在流的末尾。

方法

Close()

关闭 StreamReader 对象和基础流,并释放与读取器关联的所有系统资源。

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(Inherited from MarshalByRefObject)

DiscardBufferedData()

清除内部缓冲区。

Dispose()

释放由 TextReader 对象使用的所有资源。

(Inherited from TextReader)

Dispose(Boolean)

关闭基础流,释放 StreamReader 使用的未托管资源,同时还可以根据需要释放托管资源。

Equals(Object)

确定指定的对象是否等于当前对象。

(Inherited from Object)

GetHashCode()

作为默认哈希函数。

(Inherited from Object)

GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。

(Inherited from MarshalByRefObject)

GetType()

获取当前实例的 Type

(Inherited from Object)

InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。

(Inherited from MarshalByRefObject)

MemberwiseClone()

创建当前 Object 的浅表副本。

(Inherited from Object)

MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(Inherited from MarshalByRefObject)

Peek()

返回下一个可用的字符,但不使用它。

Read()

读取输入流中的下一个字符并使该字符的位置提升一个字符。

Read(Char[], Int32, Int32)

从当前流中将指定的最多个字符读到指定索引位置开始的缓冲区。

ReadAsync(Char[], Int32, Int32)

从当前流中异步读取指定的最大字符,并且从指定的索引位置开始将该数据写入缓冲区。

ReadBlock(Char[], Int32, Int32)

从当前流中读取指定数目的字符并从指定索引开始将该数据写入缓冲区。

ReadBlockAsync(Char[], Int32, Int32)

从当前流中异步读取指定的最大字符,并且从指定的索引位置开始将该数据写入缓冲区。

ReadLine()

从当前流中读取一行字符并将数据作为字符串返回。

ReadLineAsync()

从当前流中异步读取一行字符并将数据作为字符串返回。

ReadToEnd()

从流的当前位置到末尾读取所有字符。

ReadToEndAsync()

异步读取从当前位置到流的结尾的所有字符并将它们作为一个字符串返回。

ToString()

返回表示当前对象的字符串。

(Inherited from Object)

IDisposable.Dispose()

有关此成员的说明,请参见 Dispose()

(Inherited from TextReader)