我有一个 1 GB 的文本文件,需要逐行阅读。最好和最快的方法是什么?
private void ReadTxtFile()
{
string filePath = string.Empty;
filePath = openFileDialog1.FileName;
if (string.IsNullOrEmpty(filePath))
{
using (StreamReader sr = new StreamReader(filePath))
{
String line;
while ((line = sr.ReadLine()) != null)
{
FormatData(line);
}
}
}
}
在 FormatData()
中,我检查必须与单词匹配的行的起始单词,并基于该单词递增一个整数变量。
void FormatData(string line)
{
if (line.StartWith(word))
{
globalIntVariable++;
}
}
最佳答案
如果您使用的是 .NET 4.0,请尝试 MemoryMappedFile这是为这种情况设计的类。
否则您可以使用 StreamReader.ReadLine
。
关于c# - 如何在 .NET 中读取大型 (1GB) 文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4273699/