Here is my situation.
- Read one line from a text file
- "Process" the line
- Write the "processed" line to a new text file
- Loop to #1 and repeat until EOF
Here is how I'm doing it:
using (StreamReader srReader = new StreamReader(strInputFile))
{
// loop until EOF
while ((strCurrentLine = srReader.ReadLine()) != null)
{
// "process" strCurrentLine...
// write the "processed" strCurrentLine to a new text file
using (StreamWriter sw = new StreamWriter(strFullOutputPathFileName, false))
{
// write strCurrentLine to the new file
sw.WriteLine("stuff...");
}
}
}
My manager tells me that using the using statement like I am inside of a loop will extremely hinder performance. The reason is because the StreamWriter instance will be created as many times as I'm looping. So, if I looped 1,000 times, I'd have 1,000 instances of my StreamWriter object, which could severely hinder performance.
Is this true? Also, is my method above the best way to accomplish this?
I'd have 1,000 instancesWell, no. But you'd have created one 1000 times.File.ReadAllText()andFile.WriteAllText()overStreamReaderandStreamWriter.