StreamReader类
2024-10-21 07:49:47
StreamReader类用于从文件中读取数据,该类是一个通用类,可用于任何流,构造方法和StreamWrite类格式一样的。
创建方式有两种:
1.先创建Filestream类在创建StreamReader类
FIlestream a=new FileStream(string path,FileMode mode);
StreamReader sd=new StreamReader(a);
2.直接创建StreamReader类
StreamReader sd=new StreamReader(string path);
StreamReader 类以一种特定的编码输入字符,而StreamReader类可读取标准的文本文件的各行信息,StreamReader的
默认编码为UTF-8,UTF-8可以正确的处理Unicode字符并在操作系统的本地化版本上提供一直的结果。
StreamReader类的常用方法
Close 关闭当前StreamReader对象和基础流
Dispose 释放使用的所有资源
Peek 返回下一个可用的字符
Read 读取输入流中的下一个字符或下组字符
ReadLine 从数据流中读取一行数据,并作为字符串返回
实例: 找到Host文件 并读取到屏幕上
class Program
{
static void Main(string[] args)
{
string path = @"C:\Windows\System32\drivers\etc\hosts";//文件路径
string read="";//定义字符串read接收读取流
if (File.Exists(path))
{
//using(){} 自动帮助我们释放流所占用的空间
//()创建过程 {}读取或写入过程 均不能加分号;
using(StreamReader sd = new StreamReader(path))
{
read = sd.ReadLine();
while (read!=null)
{
Console.WriteLine(read);
read = sd.ReadLine();
}
} }
else
{
Console.WriteLine("没有找到要读取的文件");
}
Console.Read();
}
}
最新文章
- False 等效值
- TimeVal类——Live555源码阅读(一)基本组件类
- mybatis延迟加载
- iOS开发拓展篇—UIDynamic(简单介绍)
- oracle 序列 详解
- oracle游标循环的嵌套
- 在VMware中安装RHEL6.2(下)—— RHEL系统安装
- IOS UIView子类UIScrollView
- POJ 3592 Instantaneous Transference(强联通分量 Tarjan)
- HDU-2952 Counting Sheep (DFS)
- RILC
- PHP源码阅读strtr
- Linux Shell 命令--awk
- 万水千山ABP - 时区问题
- formData 对象 与 Content-Type 类型
- C++ 数组和字符串
- git 实战
- spring+shiro+ehcache整合
- jdk升级到9,eclipse打不开
- Persona——Web人物角色介绍
热门文章
- 运维利器:钉钉机器人脚本告警(Linux Shell 篇)
- Kotlin 数据类型(数值类型)
- BZOJ 1974 [Sdoi2010] auction 代码拍卖会(数位dp)
- 2019.2.25考试T1, 矩阵快速幂加速递推+单位根反演(容斥)
- MVC与三层的区别
- 利用zookeeper生成唯一id
- Git的安装使用
- 【Cracking the Code Interview(5th edition)】二、链表(C++)
- Kibana6.x.x——导航权限控制入门
- 华东交通大学2015年ACM“双基”程序设计竞赛1001