【问题标题】:Writing to txt file reach out of memory ? C# [closed]写入txt文件内存不足? C# [关闭]
【发布时间】:2017-11-01 14:34:34
【问题描述】:

我正在尝试在 C# 应用程序中向 .txt 文件中写入大量行,大约 20-30000 行:

class Program
{
  static void Main(string[] args)
  {   
    var watch = System.Diagnostics.Stopwatch.StartNew();

    // the code that you want to measure comes here
    List <string> firstListTrade= new List<string>();
    List <string> secondListTrade= new List<string>();
    firstListTrade= System.IO.File.ReadAllLines(@"C:\Users\me\Desktop\File1.txt").ToList();
    secondListTrade=System.IO.File.ReadAllLines(@"C:\Users\me\Desktop\Fil2.txt").ToList();

    string resultOne = "C:\\Users\\me\\Desktop\\resultOutput1.txt";
    string resultatsTwoo = "C:\\Users\\elbb001\\Desktop\\resultOutput2.txt";

    //Sorting lists
    firstListTrade=  firstListTrade.OrderBy(q => q).ToList();
    secondListTrade= secondListTrade.OrderBy(q => q).ToList();

    // Write the string array to a new file named "WriteLines.txt".
    StreamWriter outputFileOne = new StreamWriter(resultatsOne);
    StreamWriter outputFileTwoo = new StreamWriter(resultatsTwoo);
    int i = firstListTrade.Count();
    int j = secondListTrade.Count();
    int endofFile = 0;
    foreach (string trade in secondListTrade)
    {
      endofFile++;
      if (!firstListTrade.Contains(trade))
      {
        outputFileOne.WriteLine("Number : " + trade + " exist in first list but not second");
      }
      if(endofFile==i)
      {
        outputFileOne.WriteLine("End of file : " + endofFile);
      }

      outputFileOne.Flush();
    }

    endofFile = 0;
    foreach (string trade in firstListTrade)
    {
      endofFile++;
      if (!secondListTrade.Contains(trade))
      {
        outputFileTwoo.WriteLine("Number : " + trade + " exist in second but not in first ");
      }

      if (endofFile == j)
      {
        outputFileTwoo.WriteLine("End of file : "+ endofFile);
      }
    }

    watch.Stop();
    var elapsedMs = watch.ElapsedMilliseconds;
    // Keep the console window open in debug mode.
    Console.WriteLine("Done in  : " + elapsedMs.ToString());
    System.Console.ReadKey();
  }
}

我编译并没有收到错误,但是当我打开文件时,我看到了我想要的结果,但是当我一直向下滚动时,我注意到文件末尾有这句话:

" 交易号:22311"

但是当我在文件末尾使用断点时,它是在代码中到达但没有写入文件中?

可能出了什么问题?它是否超出了内存?还是txt文件不能再写了?

【问题讨论】:

  • 请发布更完整的示例。 otherTradeList 的类型是什么? i 变量的意义是什么?帮助我们帮助您。
  • **BLANK BLANK BLANK* 行来自某个地方,而不是来自框架。你正在做的事情是创造它。
  • 没有 lib 类同时具有 Contains() 和 WriteLine()
  • 人们可能会对您投反对票,因为您的代码示例仍然不完整,并且所选答案并不能真正解释问题所在。该网站不仅可以帮助找到您的问题的下一个人,还可以帮助您。如果您扩展您的示例,我们可以解决根本问题,这对于网站来说将是一个更有价值的问题。
  • 按要求格式化 - 正如我之前所说,虽然你宁愿解决真正的问题而不是隐藏它 - 我注意到在你的第一个循环中你有 Flush,但在第二个循环中没有 - 所以你可以在第二个文件中遇到同样的问题。请参阅不需要调用 Flush 的更新答案。您还可以通过使用 Linq Except 方法来简化您的代码,以查找在一个列表中但不在另一个列表中的所有条目。

标签: c# .net


【解决方案1】:

根据新问题,答案如下:

StreamWriter outputFileTwoo = new StreamWriter(resultatsTwoo);
List <string> firstListThatIcantRevealItName= new List<string>();
List <string> secondListThatIcantRevealItName= new List<string>();
firstListThatIcantRevealItName=System.IO.File.ReadAllLines(@"C:\Users\me\Desktop\blabla.txt").ToList();
secondListThatIcantRevealItName=System.IO.File.ReadAllLines(@"C:\Users\me\Desktop\potto.txt").ToList();

using(StreamWriter outputFileOne = new StreamWriter(resultatsOne))
{
  foreach (string trade in secondListThatIcantRevealItName)
  {
    endofFile++;
    if (!secondListThatIcantRevealItName.Contains(trade))
    {
      outputFileOne.WriteLine("Trade number : " + trade + " exist in first list  but not in second list ");
    }
    if(endofFile==i)
    {
      outputFileOne.WriteLine(endofFile);
    }
  }
}

【讨论】:

  • 除了对Flush() 的通常不必要的调用之外,还有什么变化?这只有在代码没有关闭流(即错误)时才有帮助。 Flushing 只是掩盖了 bug,并没有删除它
  • @user3752718 不,它没有,它掩盖了它。您是否记得在退出程序之前关闭流?还是因为仍在输出缓冲区中而丢失了 4K 数据?那是你的代码的一个错误。 Flush() 通过在每次迭代中强制缓冲区退出并损害性能来掩盖它
  • @user3752718 如果Flush 有帮助,您应该删除它并将otherTradeList 的定义放在using 块中。 将保证即使您忘记调用Close() 也会写出数据
  • @user3752718 不,不是。列表没有 FlushWriteLine 方法。发布您的代码。您忘记关闭您的 Streamwriter 和您的流
  • 请检查编辑。
猜你喜欢
  • 2016-10-22
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多