c# - 如何在 .NET 中读取大型 (1GB) 文本文件?

标签 c# .net streamreader

我有一个 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/

相关文章:

c# - 集合上的表达式树 'Any'

c# - 无法使用从 skydrive 获取的文件流来创建 BitmapImage

.net - 从 Visual Studio 部署 .NET 应用程序以供公众使用的正确且完整的步骤是什么?

c# - StreamReader 奇怪的错误与�

c# - 在 ASP.NET 中异步填充数据集或数据表的最佳做法是什么?

c# - 如何在没有对象的情况下访问 Page 类的属性(实例)

c# - 通过 NDepend 查找仅在特殊类中使用的代码

c# - 使用区分文化的比较从字符串中获取子字符串

c# - 是否需要创建多个streamreader实例来读取多个文件?

c# - 使用 streamreader 处理异常时遇到问题