我有幾個文件的內容需要合併到一個文件中。我有下面的代碼可以做到這一點......但從內存使用情況來看,它似乎相當低效......你會建議一個更好的方法來做到這一點嗎?內存高效的文件追加
的Util.MoveFile功能只是佔整個卷
private void Compose(string[] files) { string inFile = ""; string outFile = "c:\final.txt"; using (FileStream fsOut = new FileStream(outFile + ".tmp", FileMode.Create)) { foreach (string inFile in files) { if (!File.Exists(inFile)) { continue; } byte[] bytes; using (FileStream fsIn = new FileStream(inFile, FileMode.Open)) { bytes = new byte[fsIn.Length]; fsIn.Read(bytes, 0, bytes.Length); } //using (StreamReader sr = new StreamReader(inFile)) //{ // text = sr.ReadToEnd(); //} // write the segment to final file fsOut.Write(bytes, 0, bytes.Length); File.Delete(inFile); } } Util.MoveFile(outFile + ".tmp", outFile);
}
也許看到:在C#中連接三個文件的最快方法是什麼? http://stackoverflow.com/questions/444309/what-would-be-the-fastest-way-to-concatenate-ree-files-in-c – 2010-06-01 02:57:36
你爲什麼覺得效率低下? – 2010-06-01 02:57:42
如果這些是文本文件,爲什麼不使用「貓?」 http://en.wikipedia.org/wiki/Cat_%28Unix%29 – Alan 2010-06-01 02:57:48