C#遍历TXT每一行数据
可以使用StreamReader类来读取TXT文件的每一行数据。以下是一个示例代码:
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "path/to/your/textfile.txt";
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
在上面的示例中,需要将 path/to/your/textfile.txt 替换为实际的TXT文件路径。然后,我们使用 StreamReader 类来打开文件并逐行读取数据。ReadLine 方法用于读取文件的下一行,并将其赋值给 line 变量。使用 while 循环来遍历文件的每一行,直到到达文件的末尾。在循环中,我们可以根据需要对每一行的数据进行处理。在示例代码中,我们简单地将每一行数据打印到控制台上,你可以根据需要进行修改
原文地址: http://www.cveoy.top/t/topic/iYJt 著作权归作者所有。请勿转载和采集!