我想知道如何在不使用太多系统资源的情况下拆分大文件.我目前正在使用此代码:
public static void SplitFile(string inputFile, int chunkSize, string path)
{
byte[] buffer = new byte[chunkSize];
using (Stream input = File.OpenRead(inputFile))
{
int index = 0;
while (input.Position < input.Length)
{
using (Stream output = File.Create(path + "\\" + index))
{
int chunkBytesRead = 0;
while (chunkBytesRead < chunkSize)
{
int bytesRead = input.Read(buffer,
chunkBytesRead,
chunkSize - chunkBytesRead);
if (bytesRead == 0)
{
break;
}
chunkBytesRead += bytesRead;
}
output.Write(buffer, 0, chunkBytesRead);
}
index++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
该操作需要52.370秒才能将1.6GB文件拆分为14mb文件.我不关心操作需要多长时间,我更关心使用的系统资源,因为这个应用程序将部署到共享托管环境.目前,此操作最大化了我的系统HDD IO使用率100%,并大大减慢了我的系统速度.CPU使用率低; RAM略微上升,但看起来很好. …
我有多个文本文件需要阅读并合并到一个文件中.这些文件大小不一:每个1到50 MB.什么是组合这些文件而不会陷入恐惧的最有效方法System.OutofMemoryException?