首页  >  文章  >  后端开发  >  C# 流读取器

C# 流读取器

WBOY
WBOY原创
2024-09-03 15:23:26373浏览

为了将字符读取到遵循特定编码的流中,我们使用 C# 中称为 StreamReader 类的类,StreamReader 类的 StreamWriter.Read() 方法负责读取下一个字符或下一组流中的字符数。 TextReader类是StreamReader类的基类,StreamReader类继承自TextReader类,这个TextReader类提供了几个可用于读取字符、块、行等的方法,System.IO.namespace是定义 StreamReader 的命名空间,StreamReader 类提供了多种读取方法,例如 Peak、Read、ReadAsync、ReadBlock、ReadBlockAsync、ReadLine、ReadLineAsync、ReadToEnd、ReadToEndAsync 等

语法:

C#中StreamReader类的语法如下:

public class StreamReader: System.IO.TextReader

C#中StreamReader类的函数

  • 使用 C# 中的 Streams 从文件中读取数据。
  • 流是应用程序和文件之间的额外层。
  • 使用流可以顺利读取文件中的数据。
  • 流接收从大文件中分解出来的小数据块。应用程序可以从流中读取这些小数据块,而不必直接从较大的文件中读取所有数据
  • 如果应用程序尝试从较大文件中读取所有数据,则会影响应用程序的性能。
  • 因此,数据必须通过流读取,而不是大文件本身。

考虑下面的示例来解释 StreamReader 从文件中读取数据的用法:

C# StreamReader 示例

以下是c# StreamReader的示例

示例#1

代码:

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//the path of the file and the file name is assigned to a string variable
String def = @"D:\imp.txt";
//an instance of the string writer class is created, and the path of the file is passed as a parameter to append text to the file
using (StreamWriter stwr = File.AppendText(def))
{
//data to be appended to the file is included
stwr.WriteLine("Welcome to StreamWriter class in C#");
//the instance of the streamwriter class is closed after writing data to the File
stwr.Close();
try
{
// an instance of stream reader class is created, and data is read from the file by taking the path of the file as parameter
using (StreamReader read = new StreamReader(def))
{
//a string variable is defined
string line1;
// Data is being read one line after the other
while ((line1 = read.ReadLine()) != null)
{
Console.WriteLine(line1);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}
}

输出:

C# 流读取器

上面的程序中,程序就是定义的命名空间。然后检查是否定义了类。然后调用main方法。然后字符串变量分配文件名和文件路径。然后创建字符串编写器类实例,并将文件路径作为参数传递以将文本写入文件。然后包括要写入文件的数据。然后将数据写入文件后关闭流编写器类实例。然后创建一个stream reader类的实例,以文件的路径为参数从文件中读取数据。然后定义一个字符串变量。然后一行一行地读取数据。程序的输出如上面的快照所示。

示例#2

C#程序讲解StreamReader类的用法:

代码:

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
String fin = @"D:\Ex.txt"
//an instance of streamwriter class is created and the path of the file is passed as a parameter
using (StreamWriter stwf = new StreamWriter(fin))
{
//write() method of stream writer class is used to write the first line so that the next line continues from here
stwf.Write("Welcome to StreamWriter class in C# and ");
//writeline() method is used to write the second line and the next line starts from a new line
stwf.WriteLine("this program is demonstration of StreamWriter class in C# ");
//writeline() method is used to write the third line and the next line starts from a new line
stwf.WriteLine("I hope you are learning ");
stwf.Dispose();
try
{
// an instance of stream reader class is created, and data is read from the file by taking the path of the file as parameter
using (StreamReader read = new StreamReader(fin))
{
//a string variable is defined
string line1;
// Data is being read one line after the other
while ((line1 = read.ReadLine()) != null)
{
Console.WriteLine(line1);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}

输出:

C# 流读取器

上面的程序中,程序就是定义的命名空间。然后检查是否定义了类。然后调用main方法。然后字符串变量分配文件名和文件路径。然后创建字符串编写器类实例,并将文件路径作为参数传递以将文本写入文件。然后使用流编写器类的 write() 方法写入第一行,以便下一行从这里继续。然后使用writeline()方法写入第二行,下一行另起一行。然后使用writeline()方法写入第三行,下一行另起一行。然后创建一个stream reader类的实例,以文件的路径为参数从文件中读取数据。然后定义一个字符串变量。然后一行一行地读取数据,直到行尾。程序的输出如上面的快照所示。

以上是C# 流读取器的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# StringWriter下一篇:C# StreamWriter