StreamReader类
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();
}
}
StreamReader类的更多相关文章
- c#StreamWriter,StreamReader类(主要用于文本文件访问)
1.为什么要使用StreamReader或者StreamWriter 如果对文本文件需要读取一部分显示一部分则使用FileStream会有问题,因为可能FileStream会在读取的时候把一个汉字的字 ...
- C#--I/O流操作文本文件之StreamWrite类和StreamReader类
使用I/O流操作文本文件时主要用到StreamWrite类和StreamRead类. 1.StreamWrite类 (1)StreamWrite类专门用来处理文本文件的类.能够方便地想文本文件里写入字 ...
- C# 运用StreamReader类和StreamWriter类实现文件的读写操作
对文件的读写操作应该是最重要的文件操作,System.IO命名空间为我们提供了诸多文件读写操作类,在这里我要向大家介绍最常用也是最基本的StreamReader类和StreamWriter类.从这两个 ...
- C#中StreamReader类读取文件使用示例
C#中StreamReader类读取文件使用示例 1.需要导入的命名空间是:System.IO; 2.操作的是字符,所以打开的是文本文件. 常用属性: CurrentEncoding:对象正在使用 ...
- C# 一些知识点总结(二)_路径类,编码类,文件类...
Path 类:路径类path.GetFileName("文件路径")//获取完整文件名,包括文件名和文件拓展名Path.GetFileNameWithoutExtension(&q ...
- StreamReader和StreamWrite和FileStream区别和用法
一.<1>StreamReader类共有10个构造函数 StreamReader (Stream) // 为指定的流初始化 StreamReader 类的新实例. FileStre ...
- 一个可创建读取日志的管理类(可固定创建2M大小的日志文件)
这里,将日志管理基类命名为LogManagerBase(抽象类),具体的不同类型的日志可以通过继承完成.该基类可将日志以每个2M的方式存储起来,并可以读取当前正在使用的日志的所有内容. 要实现该基类, ...
- C#必须掌握的系统类
系统类 Type类,Object类,String类, Arrary类,Console类, Exception类,GC类, MarshalByRefObject类, Math类. DateTime结构 ...
- StreamReader与StreamWriter
StreamReader实现了抽象基类TextReader类,而StreamWriter实现了抽象基类TextWriter.分别用于对流的读取与写入. 先从StreamReader说起 一.构造方法 ...
随机推荐
- Python中dataframe\ array\ list相互转化
import pandas as pd import numpy as np #创建列表 a1=[1,2,3] #arange函数:指定初始值.终值.步长来创建数组 a2=np.arange(0,1, ...
- Velodyne VPL16 configuration in ROS Kinetic
1. 驱动安装 sudo apt-get install ros-kinetic-velodyne 2. 在已有工作空间catkin_ws中,添加Velodyne包 cd ~/catkin_ws/sr ...
- [poj 2106] Boolean Expressions 递归
Description The objective of the program you are going to produce is to evaluate boolean expressions ...
- Python导入命令 import from
一 module通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc" ...
- pytorch实现squeezenet
squeezenet是16年发布的一款轻量级网络模型,模型很小,只有4.8M,可用于移动设备,嵌入式设备. 关于squeezenet的原理可自行阅读论文或查找博客,这里主要解读下pytorch对squ ...
- 【Leetcode】Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- mysql 常用函数。。
FIND_IN_SET(str,strlist) ,strlist 是 一个 由 逗号 分割的字符串,要注意 strlist 不能有逗号.. 它 等于 where str in (1,2,3***) ...
- sublime中正则替换
匹配 <header></header> , “.” 是匹配任意 非 换行 符号 而 \s\S 匹配任何符号 匹配div class为navbar 的 div <d ...
- eclipse 自定义 文档
在这里写....
- lintcode - 被围绕的区域
class Solution { public: /* * @param board: board a 2D board containing 'X' and 'O' * @return: nothi ...