File.AppendText() 慢
File.AppendText() Slow
我有一个大文件,其结构如下:
Report 1 section 1
Report 2 section 1
Report 1 section 2
Report 3 section 1
Report 2 section 2
and so on....
我必须把所有的1放在一起,所有的2放在一起,依此类推,形成报表1、报表2、报表3。我别无选择,只能逐行进行。问题是它非常慢。这是我用来编写文件的代码:
using (StreamWriter sw = File.AppendText(newFileName))
{ sw.WriteLine(line); }
我认为问题是 File.AppendText() 正在减慢这个过程。我想知道是否有人对如何加快速度有任何想法。
您似乎在每次迭代时都打开该文件。试试这个:
using (StreamWriter sw = File.AppendText(path))
{
while (condition)
{
sw.WriteLine("write your line here");
}
}
正如 Chris Berger 评论的那样,您可以像这样嵌套使用
using (StreamWriter sw1 = File.AppendText(path1))
{
using (StreamWriter sw2 = File.AppendText(path2))
{
while (condition)
{
if(writeInFile1)
sw1.WriteLine("write your line here");
else
sw2.WriteLine("write your line here");
}
}
}
正如您在
中提到的
This is a good solution however I will have five or six report files coming from one file...
您可以使用多个 using 语句一次打开所有 6 个文件。
using (StreamReader sr = File.OpenText(source)
using (StreamWriter sw1 = File.AppendText(path1))
using (StreamWriter sw2 = File.AppendText(path2))
using (StreamWriter sw3 = File.AppendText(path3))
using (StreamWriter sw4 = File.AppendText(path4))
using (StreamWriter sw5 = File.AppendText(path5))
using (StreamWriter sw6 = File.AppendText(path6))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if(line.StartsWith("Report 1")
{
sw1.WriteLine(line);
}
else if(line.StartsWith("Report 2")
{
sw2.WriteLine(line);
}
else if(line.StartsWith("Report 3")
{
sw3.WriteLine(line);
}
else if(line.StartsWith("Report 4")
{
sw4.WriteLine(line);
}
else if(line.StartsWith("Report 5")
{
sw5.WriteLine(line);
}
else if(line.StartsWith("Report 6")
{
sw6.WriteLine(line);
}
else
{
throw new InvalidDataException($"Line does not start with a report number: \n{line}");
}
}
}
我有一个大文件,其结构如下:
Report 1 section 1
Report 2 section 1
Report 1 section 2
Report 3 section 1
Report 2 section 2
and so on....
我必须把所有的1放在一起,所有的2放在一起,依此类推,形成报表1、报表2、报表3。我别无选择,只能逐行进行。问题是它非常慢。这是我用来编写文件的代码:
using (StreamWriter sw = File.AppendText(newFileName))
{ sw.WriteLine(line); }
我认为问题是 File.AppendText() 正在减慢这个过程。我想知道是否有人对如何加快速度有任何想法。
您似乎在每次迭代时都打开该文件。试试这个:
using (StreamWriter sw = File.AppendText(path))
{
while (condition)
{
sw.WriteLine("write your line here");
}
}
正如 Chris Berger 评论的那样,您可以像这样嵌套使用
using (StreamWriter sw1 = File.AppendText(path1))
{
using (StreamWriter sw2 = File.AppendText(path2))
{
while (condition)
{
if(writeInFile1)
sw1.WriteLine("write your line here");
else
sw2.WriteLine("write your line here");
}
}
}
正如您在
This is a good solution however I will have five or six report files coming from one file...
您可以使用多个 using 语句一次打开所有 6 个文件。
using (StreamReader sr = File.OpenText(source)
using (StreamWriter sw1 = File.AppendText(path1))
using (StreamWriter sw2 = File.AppendText(path2))
using (StreamWriter sw3 = File.AppendText(path3))
using (StreamWriter sw4 = File.AppendText(path4))
using (StreamWriter sw5 = File.AppendText(path5))
using (StreamWriter sw6 = File.AppendText(path6))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if(line.StartsWith("Report 1")
{
sw1.WriteLine(line);
}
else if(line.StartsWith("Report 2")
{
sw2.WriteLine(line);
}
else if(line.StartsWith("Report 3")
{
sw3.WriteLine(line);
}
else if(line.StartsWith("Report 4")
{
sw4.WriteLine(line);
}
else if(line.StartsWith("Report 5")
{
sw5.WriteLine(line);
}
else if(line.StartsWith("Report 6")
{
sw6.WriteLine(line);
}
else
{
throw new InvalidDataException($"Line does not start with a report number: \n{line}");
}
}
}