1.ReadLine()
当遇到\n \r 或者是\r\n的时候 此方法返回这前面的字符串,然后内部的指针往后移一位下次从新的地方开始读,直到遇到数据的结尾处返回null,所以经常这样使用
       String content;
using(StreamReader sr = new StreamReader("test.txt"))
{
content=sr.ReadLine();
while(null != content)
{
  content=sr.ReadLine();
}
  sr.Close();
 
}
 
2.ReadToEnd()
这个方法适用于小文件的读取,一次性的返回整个文件, 这似乎非常容易和方便,但必须小心。将所有的数据读取到字符串对象中,会迫使文件中的数据放到内存中。应根据数据文件的大小禁止这样处理。如果数据文件非常大,最好将数据留在文件中,并使用StreamReader的方法访问文件。
上文修改如下:
try
{
  StreamReader sr = new StreamReader("test.txt");
  String content = sr.ReadToEnd();
  Debug.WriteLine();
  sr.Close();
}
catch(IOException e)
{
  Debug.WriteLine(e.ToString());
}
 
 
3  Read()
此方法每次读取一个字符,返回的是代表这个字符的一个正数,当独到文件末尾时返回的是-1
try
{
StreamReader sr = new StreamReader("test.txt");
int content=sr.Read();
while(-1 != content)
{
Debug.Write(Convert.ToChar(content));
content=sr.Read();
}
sr.Close();
}
catch(IOException e)
{
Debug.WriteLine(e.ToString());
}
 
此处需要补充一点,Read()还有一个使用方法

int Read(char[] buffer,int index,int count);

//补充一下,假设制定每次读128个字符,当文件内容小于128时,它会再循环一遍,从头开始读,直到读够128个字符

从文件流的第index个位置开始读,到count个字符,把它们存到buffer中,然后返回一个正数,内部指针后移一位,保证下次从新的位置开始读。
举个使用的例子:

try
{
StreamReader sr = new StreamReader("test.txt");
char[] buffer=new char[128];
int index=sr.Read(buffer,0,128);
while(index>0)
{
String content = new String(buffer,0,128);
Debug.Write(content);
index=sr.Read(buffer,0,128);
}
sr.Close();
}
catch(IOException e)
{
Debug.WriteLine(e.ToString());
}
 
原文链接: http://blog.csdn.net/kaituozhe345/article/details/7334988

StreamReader 的ReadLine,Read,ReadToEnd方法的更多相关文章

  1. java-IO流-字符流-FileReader、FileWriter、自定义小数组的拷贝、BufferedReader、BufferedWriter、readLine()和newLine()方法、LineNumberReader、使用指定的码表读写字符

    ###21.01_IO流(字符流FileReader) * 1.字符流是什么     * 字符流是可以直接读写字符的IO流     * 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要 ...

  2. 逐行读取txt文件,使用Linq与StreamReader的Readline方法

    List<string[]> list = File.ReadLines("YourFile.txt") .Select(r => r.TrimEnd('#')) ...

  3. 关于StreamReader.ReadToEnd方法

    以前写抓取网页的代码喜欢用ReadToEnd,因为简单省事,后来发现,在爬取网页的时候,如果网速很慢,ReadToEnd超时的几率很大.使用Read改写后,超时几率大大减小,完整代码如下: /// & ...

  4. python中读取文件的read、readline、readlines方法区别

    #读取文件所有内容,返回字符串对象,python默认以文本方式读取文件,遇到结束符读取结束. fr = open('lenses.txt')read = fr.read()print(type(rea ...

  5. 2019-2-20C#开发中常用加密解密方法解析

    C#开发中常用加密解密方法解析 一.MD5加密算法 我想这是大家都常听过的算法,可能也用的比较多.那么什么是MD5算法呢?MD5全称是 message-digest algorithm 5[|ˈmes ...

  6. C#--I/O流操作文本文件之StreamWrite类和StreamReader类

    使用I/O流操作文本文件时主要用到StreamWrite类和StreamRead类. 1.StreamWrite类 (1)StreamWrite类专门用来处理文本文件的类.能够方便地想文本文件里写入字 ...

  7. C#常用字符串加解密方法封装

    C#中常用的字符串加密.解密方法封装,包含只加密但不解密的方法.收藏起来备用. //方法一 //须添加对System.Web的引用 //using System.Web.Security; /// & ...

  8. C#中常用的字符串加密,解密方法封装,包含只加密,不解密的方法

    //方法一//须添加对System.Web的引用//using System.Web.Security;/// <summary>/// SHA1加密字符串/// </summary ...

  9. C#加密解密算法汇总(转)

    方法一:     //须添加对System.Web的引用     using System.Web.Security;           ...           /// <summary& ...

随机推荐

  1. Spring Security Hello World Example--reference

    In this tutorial, we will see how we can use Spring security to protect specific resources. This Hel ...

  2. WPF App.xaml.cs常用模板,包括:异常捕获,App只能启动一次

    App.xaml.cs中的代码每次都差不多,故特地将其整理出来直接复用: using System; using System.Configuration; using System.Diagnost ...

  3. 迁移桌面程序到MS Store(7)——APPX + Service

    本篇我们以一个Sample工程,来说明如何把一个常见结构的desktop application,转制成APPX并在MS Store提供下载. 之前的篇章中,我们已经介绍了一些内容,包括如何通过Vis ...

  4. UWP 2018 新版 NavigationView 尝鲜

    本文参考了官方文档以及提供的示例代码(官方代码貌似有点误导,所以写了这一篇,并且文末有GayHub代码地址) 官方文档发布于20180806,说明NavigationView刚发布了没几天,还在开发中 ...

  5. 友链&&日记

    上面友链,下面日记 友人链 最喜欢galgameの加藤聚聚 初三一本&&\(ACG\)姿势比我还丰厚的yx巨巨 更喜欢galgame的shadowice czx ZigZag胖胖 文文 ...

  6. PHP中正则表达式函数(Perl兼容)

    PHP为使用Perl兼容的正则表达式搜索字符串提供了7个函数,分别是preg_grep().preg_match().preg_match_all().preg_quote().preg_replac ...

  7. Eleasticsearch启动失败问题解决

    问题: [root@dnode1 bin]# ./elasticsearch -d [root@dnode1 bin]# Exception in thread "main" ja ...

  8. Spring Boot 中使用 Jedis 及 Lettuce的对比

    首先,同样的程序,采用不同方式的Redis连接方式. defautl : 默认,0配置 ,也就是走的是 lettuce 单通道方式.   端口:8081 jedis : 使用Jedis 连接池.    ...

  9. MethodImplOptions.Synchronized的一点讨论

    Review代码发现有一个方法加了[MethodImpl(MethodImplOptions.Synchronized)] 属性,这个属性的目的,从名字上就可以看出,是要对所有线程进行同步执行. 对方 ...

  10. 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 0、学习目标

    1. Build a logistic regression model, structured as a shallow neural network2. Implement the main st ...