C# StreamReader类实现读取文件的方法
作者:c语言中文网 时间:2023-06-20 13:02:00
在 C# 语言中 StreamReader 类用于从流中读取字符串。它继承自 TextReader 类。
StreamReader 类的构造方法有很多,这里介绍一些常用的构造方法,如下表所示。
构造方法 | 说明 |
---|---|
StreamReader(Stream stream) | 为指定的流创建 StreamReader 类的实例 |
StreamReader(string path) | 为指定路径的文件创建 StreamReader 类的实例 |
StreamReader(Stream stream, Encoding encoding) | 用指定的字符编码为指定的流初始化 StreamReader 类的一个新实例 |
StreamReader(string path, Encoding encoding) | 用指定的字符编码为指定的文件名初始化 StreamReader 类的一个新实例 |
使用该表中的构造方法即可创建 StreamReader 类的实例,通过实例调用其提供的类成 员能进行文件的读取操作。
StreamReader 类中的常用属性和方法如下表所示。
属性或方法 | 作用 |
---|---|
Encoding CurrentEncoding | 只读属性,获取当前流中使用的编码方式 |
bool EndOfStream | 只读属性,获取当前的流位置是否在流结尾 |
void Close() | 关闭流 |
int Peek() | 获取流中的下一个字符的整数,如果没有获取到字符, 则返回 -1 |
int Read() | 获取流中的下一个字符的整数 |
int Read(char[] buffer, int index, int count) | 从指定的索引位置开始将来自当前流的指定的最多字符读到缓冲区 |
string ReadLine() | 从当前流中读取一行字符并将数据作为字符串返回 |
string ReadToEnd() | 读取来自流的当前位置到结尾的所有字符 |
下面通过实例来演示 StreamReader 类的应用。
【实例】读取 D 盘 code 文件夹下 test.txt 文件中的信息。
根据题目要求,先在 D 盘下创建文件夹并创建 test.txt 文件,然后写入两行字符,分别是 Hello 和 Lucy,代码如下。
class Program
{
static void Main(string[] args)
{
//定义文件路径
string path = @"D:\\code\\test.txt";
//创建 StreamReader 类的实例
StreamReader streamReader = new StreamReader(path);
//判断文件中是否有字符
while (streamReader.Peek() != -1)
{
//读取文件中的一行字符
string str = streamReader.ReadLine();
Console.WriteLine(str);
}
streamReader.Close();
}
}
执行上面的代码,效果如下图所示。
在读取文件中的信息时,除了可以使用 ReadLine 方法以外,还可以使用Read、ReadToEnd方法来读取。
来源:http://c.biancheng.net/view/2925.html
标签:C#,StreamReader,读取文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实战员工绩效管理系统的实现流程
2021-11-20 19:04:28
![](https://img.aspxhome.com/file/2023/1/81941_0s.jpg)
C# 中如何利用lambda实现委托事件的挂接
2022-02-06 03:18:00
C# Chart控件标记问题详解
2023-01-23 03:37:35
![](https://img.aspxhome.com/file/2023/7/119557_0s.webp)
MybatisPlus多表连接查询的问题及解决方案
2023-11-25 22:09:06
![](https://img.aspxhome.com/file/2023/8/60348_0s.png)
MyBatis中的properties配置(推荐)
2023-06-04 20:47:58
C#中的尾递归与Continuation详解
2021-12-05 16:35:15
解决idea web项目中out目录更新不同步问题
2023-01-30 01:32:28
![](https://img.aspxhome.com/file/2023/3/97813_0s.png)
Android中文件读写(输入流和输出流)操作小结
2023-07-23 02:53:19
![](https://img.aspxhome.com/file/2023/7/138147_0s.jpg)
Java ArrayList实现删除指定位置的元素
2023-11-25 05:34:13
![](https://img.aspxhome.com/file/2023/0/60160_0s.png)
实战分布式医疗挂号系统开发医院科室及排班的接口
2021-05-27 14:21:16
![](https://img.aspxhome.com/file/2023/3/68143_0s.png)
android基于SwipeRefreshLayout实现类QQ的侧滑删除
2023-05-22 14:49:19
![](https://img.aspxhome.com/file/2023/2/138352_0s.gif)
Java Validation Api使用方法实例解析
2023-05-16 05:44:58
![](https://img.aspxhome.com/file/2023/7/119107_0s.png)
java实现小猫钓鱼游戏
2021-10-10 19:59:48
C# 的析构以及垃圾回收实例分析
2021-12-22 19:03:37
Android四种常见布局方式示例教程
2022-05-25 09:35:03
![](https://img.aspxhome.com/file/2023/7/108287_0s.png)
Java设计模式之接口隔离原则精解
2022-05-05 16:42:05
![](https://img.aspxhome.com/file/2023/5/80935_0s.png)
Android编程实现图片平铺的方法分析
2022-05-27 06:37:40
Android非异常情况下的Activity生命周期分析
2022-10-29 04:06:12
![](https://img.aspxhome.com/file/2023/6/137406_0s.png)
Android实现滑动屏幕切换图片
2022-04-26 23:14:42
![](https://img.aspxhome.com/file/2023/1/138741_0s.jpg)
实例化JFileChooser对象报空指针异常问题的解决办法
2023-10-05 11:36:18
![](https://img.aspxhome.com/file/2023/7/132067_0s.png)