如何在c#中合并两个大文件? [英] How can I merge two big files in c#

查看:348
本文介绍了如何在c#中合并两个大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友



我希望你们一切顺利,



我必须合并两个大牌我的winform应用程序中的文件。首先我使用了File.WriteAllText(),但它抛出了内存超出绑定的异常。所以请帮我找一个优化的低内存使用代码来解决我的问题。



谢谢和问候

Hi friends

I hope you all doing well,

I have to merge two big file in my winform application. First I used File.WriteAllText(), But it throw memory outof bound exception. So please help me to find a optimized and low memory used code to solve my problem.

Thanks and regards

推荐答案

假设你的文件是文本文件,(或者你不会尝试使用WriteAllText),你可以通过多种方式进行操作 - 但大部分问题都集中在合并部分。

如果磁盘上有两个文件,并且你只想创建一个跟随另一个文件的第三个文件,那么它很简单:

打开输出文件,作为流,并以块的形式读取输入文件,将每个块写入输出。

Assuming your files are text files, (or you wouldn't be trying WriteAllText) there are a huge number of ways you could do it - but most of the problem is centered on the "Merge" part.
If you have two files on disk, and you want to just create a third file with one following the other, then it's pretty easy:
open the output file, as a stream, and read the input files in chunks, writing each chunk to the output.
private void butMerge_Click(object sender, EventArgs e)
    {
    using (StreamWriter sw = new StreamWriter(@"D:\Temp\Output.txt"))
        {
        AppendFile(sw, @"D:\Temp\MyHugeText1.txt");
        AppendFile(sw, @"D:\Temp\MyHugeText2.txt");
        }
    }

private void AppendFile(StreamWriter sw, string path)
    {
    int blockSize = 1024 * 1024 * 8;
    char[] block = new char[blockSize];
    using (StreamReader sr = new StreamReader(path))
        {
        while (!sr.EndOfStream)
            {
            int chars = sr.Read(block, 0, blockSize);
            sw.Write(block, 0, chars);
            }
        }
    }


const int chunkSize = 2 * 1024; // 2KB

var inputFiles = new [] {file1.dat,file2.dat,file3.dat};

using(var output = File.Open(outputfile,FileMode.Append))

{

foreach(输入文件中的strng文件)

{



使用(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);

}



}

}



}



我认为它有更好的表现..
const int chunkSize = 2 * 1024; // 2KB
var inputFiles = new[] { "file1.dat", "file2.dat","file3.dat" };
using (var output = File.Open(outputfile,FileMode.Append))
{
foreach(strng 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);
}

}
}

}

I think its have better performance ..

这篇关于如何在c#中合并两个大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆