C#如何一行一行读取指定格式的TXT数据? 比如先读取1,2,3,4,把这些数据处理后,再对下一行进行处理。

2025-02-26 03:35:25
推荐回答(2个)
回答1:

这个问题其实很简单。看代码:
StreamReader sr = new StreamReader("c:\\a.txt");
while (sr.Read())
{
string str = sr.ReadLine();
//这里处理 str
}
这样子,是读一行处理一行.明白吗?

回答2:

static void Main(string[] args)
{
string strPath = txt的位置
string line = string.Empty;
StreamReader sr = new StreamReader(strPath);
line = sr.ReadLine();
}

读到的line就是一行,你可以分割出来
你可以通过循环来读

while(( line = sr.ReadLine()) != null)...