【问题标题】:How to open a large text file in C#如何在 C# 中打开一个大文本文件
【发布时间】:2020-05-17 18:29:18
【问题描述】:

我有一个包含大约 100000 篇文章的文本文件。 文件结构为:

.文档 ID 42944-YEAR:5 .日期 03\08\11 .Cat 政治 文章内容1 .文档 ID 42945-YEAR:5 .日期 03\08\11 .Cat 政治 文章内容二

我想在 c# 中打开这个文件来逐行处理它。 我试过这段代码:

String[] FileLines = File.ReadAllText(
                  TB_SourceFile.Text).Split(Environment.NewLine.ToCharArray()); 

但它说:

类型异常 'System.OutOfMemoryException' 是 扔了。

问题是如何打开这个文件并逐行读取。

  • 文件大小:564 MB(591,886,626 字节)
  • 文件编码:UTF-8
  • 文件包含 Unicode 字符。

【问题讨论】:

    标签: c# out-of-memory text-files large-files large-text


    【解决方案1】:

    您可以打开文件和read it as a stream,而不是一次将所有内容都加载到内存中。

    来自 MSDN:

    using System;
    using System.IO;
    
    class Test 
    {
        public static void Main() 
        {
            try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) 
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您的文件太大,无法一次性读入内存,正如File.ReadAllText 正在尝试做的那样。您应该逐行读取文件。

      改编自MSDN

      string line;
      // Read the file and display it line by line.
      using (StreamReader file = new StreamReader(@"c:\yourfile.txt"))
      {
          while ((line = file.ReadLine()) != null)
          {    
              Console.WriteLine(line);
              // do your processing on each line here
          }
      }
      

      这样,任何时候在内存中的文件都不会超过一行。

      【讨论】:

        【解决方案3】:

        如果您使用的是 .NET Framework 4,System.IO.File 上有一个名为 ReadLines 的新静态方法,它返回字符串的 IEnumerable。我相信它已被添加到这个确切场景的框架中;但是,我自己还没有使用过。

        MSDN Documentation - File.ReadLines Method (String)

        Related Stack Overflow Question - Bug in the File.ReadLines(..) method of the .net framework 4.0

        【讨论】:

          【解决方案4】:

          类似这样的:

          using (var fileStream = File.OpenText(@"path to file"))
          {
              do
              {
                  var fileLine = fileStream.ReadLine();
                  // process fileLine here
          
              } while (!fileStream.EndOfStream);
          }
          
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-20
            • 1970-01-01
            • 2013-02-14
            • 2021-03-19
            • 2022-06-15
            • 1970-01-01
            • 1970-01-01
            • 2012-04-16
            相关资源
            最近更新 更多