将多个文件组合成单个文件 [英] Combine multiple files into single file

查看:139
本文介绍了将多个文件组合成单个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:
$ b $ pre $ $ $ $ $ $ $ $ $ b $ $ $ $ b string [] fileAry = Directory.GetFiles(dirPath,filePattern);

Console.WriteLine(Total File Count:+ fileAry.Length);

using(TextWriter tw = new StreamWriter(destFile,true))
{
foreach(fileAry中的字符串filePath)
{
using(TextReader tr = new StreamReader(filePath))
{
tw.WriteLine(tr.ReadToEnd());
tr.Close();
tr.Dispose();
}
Console.WriteLine(File Processed:+ filePath);
}

tw.Close();
tw.Dispose();




$ b我需要优化它,因为它非常慢: 3分钟为平均大小为40 - 50 MB的45个文件的文件。



请注意:平均45 MB的45个文件只是一个例子,它可以是 n m 大小的文件数量,其中 n &安培; m 可以是平均128 Kb。

您可以提供任何关于优化的意见吗?

解决方案

为什么不使用 Stream.CopyTo()方法

  private static void CombineMultipleFilesIntoSingleFile(string inputDirectoryPath,string inputFileNamePattern,string outputFilePath)
{
string [] inputFilePaths = Directory.GetFiles(inputDirectoryPath,inputFileNamePattern);
Console.WriteLine(Number of files:{0}。,inputFilePaths.Length);
using(var outputStream = File.Create(outputFilePath))
{
foreach(inputFilePaths中的inputFilePath)
{
using(var inputStream = File.OpenRead( inputFilePath))
{
//缓冲区大小可以作为第二个参数传递。
inputStream.CopyTo(outputStream);
}
Console.WriteLine(文件{0}已被处理。,inputFilePath);
}
}
}


Code:

static void MultipleFilesToSingleFile(string dirPath, string filePattern, string destFile)
{
    string[] fileAry = Directory.GetFiles(dirPath, filePattern);

    Console.WriteLine("Total File Count : " + fileAry.Length);

    using (TextWriter tw = new StreamWriter(destFile, true))
    {
        foreach (string filePath in fileAry)
        {
            using (TextReader tr = new StreamReader(filePath))
            {
                tw.WriteLine(tr.ReadToEnd());
                tr.Close();
                tr.Dispose();
            }
            Console.WriteLine("File Processed : " + filePath);
        }

        tw.Close();
        tw.Dispose();
    }
}

I need to optimize this as its extremely slow: takes 3 minutes for 45 files of average size 40 — 50 Mb XML file.

Please note: 45 files of an average 45 MB is just one example, it can be n numbers of files of m size, where n is in thousands & m can be of average 128 Kb. In short, it can vary.

Could you please provide any views on optimization?

解决方案

Why not just use the Stream.CopyTo() method?

private static void CombineMultipleFilesIntoSingleFile(string inputDirectoryPath, string inputFileNamePattern, string outputFilePath)
{
    string[] inputFilePaths = Directory.GetFiles(inputDirectoryPath, inputFileNamePattern);
    Console.WriteLine("Number of files: {0}.", inputFilePaths.Length);
    using (var outputStream = File.Create(outputFilePath))
    {
        foreach (var inputFilePath in inputFilePaths)
        {
            using (var inputStream = File.OpenRead(inputFilePath))
            {
                // Buffer size can be passed as the second argument.
                inputStream.CopyTo(outputStream);
            }
            Console.WriteLine("The file {0} has been processed.", inputFilePath);
        }
    }
}

这篇关于将多个文件组合成单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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