I have a rather large file consisting of several million lines and there is the need to check and remove corrupt lines from the file.
I have shamelessly tried File.ReadAllLines
but it didn't work. Then I tried to stream lines as below reading from the original file and writing to a new one. While it does the job, it does so in several hours(5+). I have read about using buffers which sounds like the only option but how am I going to keep line integrity in that way?
Solution: StreamWriter moved to outside of while. Instead of split, count is used.
using (FileStream inputStream = File.OpenRead((localFileToProcess + ".txt")))
{
using (StreamReader inputReader = new StreamReader(inputStream, System.Text.Encoding.GetEncoding(1254)))
{
using(StreamWriter writer=new StreamWriter(localFileToProcess,true,System.Text.Encoding.GetEncoding(1254)))
{
while (!inputReader.EndOfStream)
{
if ((tempLineValue = inputReader.ReadLine()).Count(c => c == ';') == 4)
{
writer.WriteLine(tempLineValue);
}
else
incrementCounter();
}
}
}
}