Pra*_*tik 16 .net c# file-io copy
码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我需要优化它,因为它非常慢:平均大小为40 - 50 Mb XML文件的45个文件需要3分钟.
请注意:45个平均45 MB的文件只是一个例子,它可以是大小n的文件m数,其中n有数千个m,平均可以是128 Kb.简而言之,它可以变化.
您能否提供有关优化的任何观点?
Ser*_*nov 36
为什么不直接使用该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 (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);
}
}
}
Run Code Online (Sandbox Code Playgroud)