Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to combine multiple text files

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 files without bumping into the dreading System.OutofMemoryException?

like image 370
Dave Harding Avatar asked Jun 10 '11 19:06

Dave Harding


2 Answers

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);
            }
        }
    }
}
like image 143
Darin Dimitrov Avatar answered Oct 20 '22 06:10

Darin Dimitrov


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);
        }
    }
}
like image 32
n8wrl Avatar answered Oct 20 '22 06:10

n8wrl