I have one big file that is structured something like this:
Report 1 section 1
Report 2 section 1
Report 1 section 2
Report 3 section 1
Report 2 section 2
and so on....
I have to put all the 1s together, all the 2s together, etc, into Report 1, Report 2, Report 3. I have no choice but to go line by line. The problem is that it is very slow. Here is the code I am using to write the files:
using (StreamWriter sw = File.AppendText(newFileName))
{ sw.WriteLine(line); }
I think the problem is that the File.AppendText() is slowing down this process. I'm wondering if anyone has any ideas about how to speed this up.
It seems you are opening that file for each iteration. Try this:
using (StreamWriter sw = File.AppendText(path))
{
while (condition)
{
sw.WriteLine("write your line here");
}
}
As Chris Berger has commented, you can nest usings like this
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");
}
}
}