StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析
2012-03-08 23:44
357 查看
首先StreamReader类的构造参数非常丰富
在这里,我觉得最常用的就是StreamReader(Stream stream)和StreamReader(String str)这两个最常用
第一个可以直接放入一个数据流,例如FileStream,而第二个更简单直接放入str例如“c:/test.txt”
重点讲的是它的三个方法的使用
1.ReadLine()
当遇到\n \r 或者是\r\n的时候 此方法返回这前面的字符串,然后内部的指针往后移一位下次从新的地方开始读
知道遇到数据的结尾处返回null
所以经常这样使用
String content;
try
{
StreamReader sr = new StreamReader("test.txt");
content=sr.ReadLine();
while(null != content)
{
Debug.WriteLine(content);
content=sr.ReadLine();
}
sr.Close();
}
catch(IOException e)
{
Debug.WriteLine(e.ToString());
}
2.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());
}
3.ReadToEnd()
这个方法适用于小文件的读取,一次性的返回整个文件
上文修改如下:
try
{
StreamReader sr = new StreamReader("test.txt");
String content = sr.ReadToEnd();
Debug.WriteLine();
sr.Close();
}
catch(IOException e)
{
Debug.WriteLine(e.ToString());
}
在这里,我觉得最常用的就是StreamReader(Stream stream)和StreamReader(String str)这两个最常用
第一个可以直接放入一个数据流,例如FileStream,而第二个更简单直接放入str例如“c:/test.txt”
重点讲的是它的三个方法的使用
1.ReadLine()
当遇到\n \r 或者是\r\n的时候 此方法返回这前面的字符串,然后内部的指针往后移一位下次从新的地方开始读
知道遇到数据的结尾处返回null
所以经常这样使用
String content;
try
{
StreamReader sr = new StreamReader("test.txt");
content=sr.ReadLine();
while(null != content)
{
Debug.WriteLine(content);
content=sr.ReadLine();
}
sr.Close();
}
catch(IOException e)
{
Debug.WriteLine(e.ToString());
}
2.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());
}
3.ReadToEnd()
这个方法适用于小文件的读取,一次性的返回整个文件
上文修改如下:
try
{
StreamReader sr = new StreamReader("test.txt");
String content = sr.ReadToEnd();
Debug.WriteLine();
sr.Close();
}
catch(IOException e)
{
Debug.WriteLine(e.ToString());
}
相关文章推荐
- StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析
- 【技术】C#入门:StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析
- C#控制台基础 streamreader 中的readtoend方法读取指定txt文件
- undefined reference to symbol xxxxx和undefined symbol:xxxx错误的原因分析以及解决方法
- .NET(C#):XmlReader和Whitespace以及MoveToContent和ReadToFollowing方法
- HttpWebReques请求StreamReader.ReadToEnd阻塞现象,以及HttpClient实现一次连接多次请求
- 关于 StreamReader 的 ReadToEnd() 方法
- iOS: NSFileHandle的方法readDataToEndOfFile
- 关于StreamReader.ReadToEnd方法
- Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法
- Hibernate one-to-one 的N+1问题分析及其解决方法
- 【MySQL】MySQL同步报错-> received end packet from server, apparent master shutdown: Slave I/O thread: Failed reading log event, reconnecting to retry报错解决和分析
- Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法
- Android中View绘制流程以及invalidate()等相关方法分析
- Android中View绘制流程以及invalidate()等相关方法分析
- 另一种Atlas Scripts Intellisense的方法以及对比与分析(转载)
- python-read()、readline()以及readlines()之间的区别
- 分析电脑死机的常见的几种原因,以及对应的解决方法
- java toString()方法以及重写的意义
- Fatal error: Call to** function read() on a non-object inC:*\*\select_templets.php on line *的解决方法