C# StreamReader对象
1.读取文件
输入流用于从外部源读取数据,在很多情况下,数据源可以是磁盘上的文件或网络的某些位置,任何可能发送数据的位置都可以是数据源,比如网络应用程序,web服务,甚至是控制台。StreamReader是一个通用类,可以用于任何流;StreamReader对象的创建方式非常类似于StreamWriter对象的创建方式。
StreamWriter类有两个基本的方法read和readLine
Read()方法将流的下一个字符作为正整数值返回,如果到达了流的结尾处,则返回-1
ReadLing()方法是读取并返回一行字符,如果返回为空,那么就是到达了流的结尾。
ReadEnd()方法读小文件最好,它直接读取完整的文件并作为字符串返回。
例:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace StreamReaderTest
{
class Program
{
static void Main(string[] args)
{
string strLine;
try
{
Console.Write("请输入文件路径及文件名:");
string mess = Console.ReadLine();
FileStream aFile = new FileStream(mess, FileMode.Open);
StreamReader sr = new StreamReader(aFile);//用FileStream对象实例化一个StreamReader对象
//strLine = sr.ReadToEnd();//读取完整的文件,如果用这个方法,就可以不用下面的while循环
strLine = sr.ReadLine();// 读取一行字符并返回
while (strLine != null)
{
Console.WriteLine(strLine);
strLine = sr.ReadLine();
}
sr.Close();
Console.ReadLine();
}
catch (IOException e)
{
Console.WriteLine("an IOexception has been thrown!");
Console.WriteLine(e.ToString());
return;
}
return;
}
}
}
2.分隔文件
读取使用逗号分隔的文件,string类提供了一种称为Split()的方法,可以用于将字符串按照提供的分隔符分隔成字符组.
例:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace CommaValues
{
class Program
{
static void Main(string[] args)
{
while (true)
{
string strLine;
string[] strArray;
char[] charArray = new Char[] { ',' };
Console.Write("请输入文件内容以逗号分隔的文件路径及文件名:");
string name = Console.ReadLine();
try
{
FileStream aFile = new FileStream(name, FileMode.Open);
StreamReader sr = new StreamReader(aFile);
strLine = sr.ReadLine();
while (strLine != null)
{
strArray = strLine.Split(charArray);
for (int x = 0; x <= strArray.GetUpperBound(0); x++)
{
Console.WriteLine(strArray[x].Trim());
}
strLine = sr.ReadLine();
}
sr.Close();
//Console.ReadLine();
}
catch (IOException e)
{
Console.WriteLine(e.ToString());
return;
}
//return;
}
}
}
}
C# StreamReader对象的更多相关文章
- 利用HttpWebRequest实现实体对象的上传
一 简介 HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性.这两个类位 于System.Net命名空间,默认情况下这个类对 ...
- StreamReader和StreamWrite和FileStream区别和用法
一.<1>StreamReader类共有10个构造函数 StreamReader (Stream) // 为指定的流初始化 StreamReader 类的新实例. FileStre ...
- StreamReader与StreamWriter
StreamReader实现了抽象基类TextReader类,而StreamWriter实现了抽象基类TextWriter.分别用于对流的读取与写入. 先从StreamReader说起 一.构造方法 ...
- StreamReader和StreamWriter说明
StreamReader/StreamWriter操作的是字符数据(char),而FileStream操作的是字节数据(byte) FileStream与StreamXXXX类的默认编码都是UTF8, ...
- C# System.IO.StreamReader
实现一个 TextReader,使其以一种特定的编码从字节流中读取字符. using System; using System.IO; class Test { public static void ...
- C#中流的读写器BinaryReader、BinaryWriter,StreamReader、StreamWriter详解【转】
https://blog.csdn.net/ymnl_gsh/article/details/80723050 C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操 ...
- c#StreamWriter,StreamReader类(主要用于文本文件访问)
1.为什么要使用StreamReader或者StreamWriter 如果对文本文件需要读取一部分显示一部分则使用FileStream会有问题,因为可能FileStream会在读取的时候把一个汉字的字 ...
- .net 流(Stream) - StreamWriter和StreamReader、BinaryReader和BinaryWriter
转自:http://www.oseye.net/user/kevin/blog/86 一.StreamWriter和StreamReader 从上一篇博文可知文件流.内存流和网络流操作的都是字节,每次 ...
- C#--I/O流操作文本文件之StreamWrite类和StreamReader类
使用I/O流操作文本文件时主要用到StreamWrite类和StreamRead类. 1.StreamWrite类 (1)StreamWrite类专门用来处理文本文件的类.能够方便地想文本文件里写入字 ...
随机推荐
- jdk tomcat
vi /etc/profile export JAVA_HOME=/usr/java/jdk1.8.0_121export JRE_HOME=/usr/java/jdk1.8.0_121/jreexp ...
- python依赖文件
生成 pip freeze >requirements.txt 安装 pip install -r requirements.txt
- 阿里云服务器 linux 怎么安装php(PHPSTUDY)开发环境
1.首先登录行云管家(https://yun.cloudbility.com/login.html) wget -c http://lamp.phpstudy.NET/phpstudy.bin //下 ...
- POJ - 3461 (kmp)
题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- UVALive - 6440
题目链接:https://vjudge.net/contest/241341#problem/G Indonesia, as well as some neighboring Southeast As ...
- IDEA/Eclipse安装 Alibaba Java Coding Guidelines 插件
为了让开发者更加方便.并且达到快速规范代码格式的目的并实行起来,阿里巴巴基于<阿里巴巴Java开发规约>手册内容,研发了一套自动化的IDE检测插件(IDEA.Eclipse).它就是Ali ...
- Python踩坑之旅其一杀不死的Shell子进程
目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4 坑后扩展 1.4.1 扩展知识 1.4.1 技术关键字 1.5 填坑总结 1.1 踩坑案例 踩坑的程序是个常驻的Agent类管理进程 ...
- 示例1-苏宁每日自动登录打卡-结合Au3
public class SuningAutoClock { public static void AutoClock() throws IOException, InterruptedExcepti ...
- c#读取word内容,c#提取word内容
Post by 54admin, 2009-5-8, Views:575 1: 对项目添加引用,Microsoft Word 11.0 Object Library 2: 在程序中添加 using W ...
- 完美解决Android在listview添加checkbox实现单选多选操作问题
在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上看上去只是改变checkbox那么简单,然而实际开发中,实现起来并不是那么得心应手.尤其当 ...