StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析
来源:互联网 发布:java uml 类图 编辑:程序博客网 时间:2024/06/05 19:30
首先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()还有一个使用方法
举个使用的例子:
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的分析
- StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析
- 【技术】C#入门:StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析
- StreamReader.ReadLine 的方法读取指定行
- read(), readline(), readlines()方法的使用
- read(), readline(), readlines()方法的使用
- Read和ReadLine及数组的输入方法示例
- Read和ReadLine及数组的输入方法示例
- python读文件的三个方法read()、readline()、readlines()详解
- Python中文件读取方法read(),readline(),readlines()的区别
- python读文件read()、readline()、readlines()三个方法的区别
- readLine方法的原理和案例分析
- python中read() readline()以及readlines()用法
- python中read() readline()以及readlines()用法
- python中read() readline()以及readlines()区别
- python中read() readline()以及readlines()区别 .
- python中read() readline()以及readlines()区别
- python中read() readline()以及readlines()用法
- android4.2 图库修剪视频后,详细信息中不显示时长
- linux文件系统之路径查找与文件系统的挂载
- python 准备工作
- 驾驶技巧 一
- hdu 1020 Encoding
- StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析
- 微信5.0 Android版飞机大战破解无敌模式手记
- Android---AlarmManager
- 手机屏幕尺寸,分辨率,密度,dpi,dip,px,sp等详解
- 队列的顺序存储及其基本操作
- eclipse把不开的解决方法
- Linux文件系统之sysfs
- 读书笔记-简约至上——交互设计四策略
- C语言字符串函数详解