实现一个 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)

C# System.IO.StreamReader的更多相关文章

  1. 详解C#中System.IO.File类和System.IO.FileInfo类的用法

    System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间.下面通过程序实例来介绍其主要属性和方法. (1) 文件打开 ...

  2. 使用匿名管道在进程间通信 (System.IO.Pipes使用)(转)

    原文地址:http://www.cnblogs.com/yukaizhao/archive/2011/08/04/system-io-pipes.html 管道的用途是在同一台机器上的进程之间通信,也 ...

  3. System.IO

                 I/O       1.文件操作:File (1)void AppendAllText(string path, string contents) (2)bool Exist ...

  4. C# System.IO.StreamWriter

    实现一个 TextWriter,使其以一种特定的编码向流中写入字符. using System; using System.Collections.Generic; using System.Linq ...

  5. C# System.IO和对文件的读写操作

      System.IO命名空间中常用的非抽象类 BinaryReader 从二进制流中读取原始数据 BinaryWriter 从二进制格式中写入原始数据 BufferedStream 字节流的临时存储 ...

  6. C# System.IO.File

    using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp ...

  7. winform 保存文件 打开文件 选择文件 字体样式颜色(流 using System.IO;)

    string filePath = ""; private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) ...

  8. .net System.IO之Stream的使用详解

    本篇文章是对.Net中System.IO之Stream的使用进行了详细的分析介绍,需要的朋友参考下 Stream在msdn的定义:提供字节序列的一般性视图(provides a generic vie ...

  9. 利用System.IO.Compression操作压缩文件

    引用: using System.IO.Compression; using (FileStream zipToOpen = new FileStream(@"D:\json.zip&quo ...

随机推荐

  1. 020100——00002_OS库

    OS 库中文文档:https://yiyibooks.cn/xx/python_352/library/os.html OS 库英文文档:https://docs.python.org/3/libra ...

  2. 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

    假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解释: 有两 ...

  3. 页面滚动到指定class样式位置

    var winTop =$(window).scrollTop(); var top = document.getElementsByClassName('is-danger')[0].getBoun ...

  4. Java常用API——String字符串运算

    一.字符串运算 String类 1.概述 String是特殊的引用数据类型,它是final类. 2.构造方法 String str = "abc"; 相当于:  char date ...

  5. TF之BN:BN算法对多层中的每层神经网络加快学习QuadraticFunction_InputData+Histogram+BN的Error_curve

    # 23 Batch Normalization import numpy as np import tensorflow as tf import matplotlib.pyplot as plt ...

  6. SVM:随机产生100个点,建立模型,找出超平面方程——Jaosn niu

    import numpy as np import pylab as pl from sklearn import svm # we create 40 separable points #np.ra ...

  7. 超出JavaScript安全整数限制的数字计算-BigInt

    JavaScript中的基本数据类Number是双精度浮点数,它可以表示的最大安全范围是正负9007199254740991,也就是2的53次方减一,在浏览器控制台分别输入Number.MAX_SAF ...

  8. 关于GitHub中文乱码的问题

    其实跟Git里中文乱码相比,这个问题好解决得多. GitHub中的乱码是由于其使用的编码与Windows记事本的默认编码不同.只要在本地将编码改成UTF-8,再重新push就可以了. 修改编码的方法是 ...

  9. JS导出gridview到excel

    <html> <head> <script type="text/javascript"> var tableToExcel = (functi ...

  10. 解决Python自带的json不能序列化data,datetime类型数据问题

    官方文档中的一个Demo: >>> import json >>> class ComplexEncoder(json.JSONEncoder): ... def ...