c# textreader
类在system.io
命名空间中定义。它表示一个可以用来读取文本或连续字符串的读取器。
下面让我们来看看,textreader
类的一个简单示例,使用它来从指定文件中读取数据直到文件结尾。
假设有一个名称为:textwriter.txt的文件,内容如下 -
hello c#, textwriter
c# file handling by h3.com
示例代码如下 -
using system;
using system.io;
namespace textreaderexample
{
class program
{
static void main(string[] args)
{
using (textreader tr = file.opentext("e:\\textwriter.txt"))
{
console.writeline(tr.readtoend());
}
}
}
}
执行上面示例代码,得到以下输出结果 -
hello c#, textwriter
c# file handling by h3.com
下面来看看如何使用textreader
类从文件中读取一行的一个简单例子。
假设用有一个文本文件:textwriter.txt,有以下内容 -
hello c#, textwriter
c# file handling by h3.com
示例代码 -
using system;
using system.io;
namespace textreaderexample
{
class program
{
static void main(string[] args)
{
using (textreader tr = file.opentext("e:\\textwriter.txt"))
{
console.writeline(tr.readline());
}
}
}
}
执行上面程序代码,得到以下结果 -
hello c#, textwriter