Efficient way to combine multiple text files

后端 未结 3 528
南笙
南笙 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: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);
                }
            }
        }
    }
    

提交回复
热议问题