这是我目前的代码
Dim Paths() As String = Directory.GetFiles("files*.txt")
For Each Path As String In Paths
File.AppendAllText("merged.txt", File.ReadAllText(Path), Encoding.Default)
Next
问题似乎在于,使用此方法时,处理多个大文件时性能较差。
有没有更有效的方式来合并文本文件?也许首先将所有文件读入流读取器,然后在一个操作中创建输出文件?
试试这个:
using (StreamWriter sw = new StreamWriter("merge.txt"))
{
string[] paths = Directory.GetFiles("files*.txt");
foreach (string path in paths)
using (StreamReader sr = new StreamReader(path))
{
sw.Write(sr.ReadToEnd());
sw.WriteLine("");
}
}
我认为慢速操作是在File.AppendAllText中,它打开->写入->关闭目录中每个txt文件的merge.txt文件
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句