写入txt文件到达内存不足? C#

时间:2017-11-01 14:34:35

标签: c# .net

我正在尝试在C#应用程序中为.txt文件写大行,大约20-30万行:

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文件不能再写了?

1 个答案:

答案 0 :(得分:2)

根据新问题,答案如下:

class MailchimpController extends Controller
{
    private $MC_API_KEY;
    private $MailChimp;

    public function __construct()
    {
        $this->CHIMP_KEY = getenv('CHIMP_KEY');
        $this->MailChimp = new MailChimp($this->CHIMP_KEY);
    }

    public function index()
    {
        return $this->MailChimp->get('test');
    }

    public function show($test)
    {
        return $this->MailChimp->get("test/$test");
    }
}