Efficient way to combine multiple text files

后端 未结 3 507
南笙
南笙 2020-12-25 13:32

I have multiple files of text that I need to read and combine into one file. The files are of varying size: 1 - 50 MB each. What\'s the most efficient way to combine these f

相关标签:
3条回答
  • 2020-12-25 14:11

    This is code used above for .Net 4.0, but compatible with .Net 2.0 (for text files)

    using (var output = new StreamWriter("D:\\TMP\\output"))
    {
      foreach (var file in Directory.GetFiles("D:\\TMP", "*.*"))
      {
        using (var input = new StreamReader(file))
        {
          output.WriteLine(input.ReadToEnd());
        }
      }
    }
    

    Please note that this will read the entire file in memory at once. This means that large files will cause a lot of memory to be used (and if not enough memory is available, it may fail all together).

    0 讨论(0)
  • 2020-12-25 14:16

    Darin is on the right track. My tweak would be:

    using (var output = File.Create("output"))
    {
        foreach (var file in new[] { "file1", "file2" })
        {
            using (var input = File.OpenRead(file))
            {
                input.CopyTo(output);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-25 14:18

    Do it in chunks:

    const int chunkSize = 2 * 1024; // 2KB
    var inputFiles = new[] { "file1.dat", "file2.dat", "file3.dat" };
    using (var output = File.Create("output.dat"))
    {
        foreach (var file in inputFiles)
        {
            using (var input = File.OpenRead(file))
            {
                var buffer = new byte[chunkSize];
                int bytesRead;
                while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    output.Write(buffer, 0, bytesRead);
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题