I’d use Streams for this if the input file is large. You will need a StreamReader
and two StreamWriters
. Put them inside a using
statement so they are closed properly when they go out of scope. Something like this.
const string path = @"C:\temp\Test.txt";
const string pathA = @"C:\temp\testA.txt";
const string pathB = @"C:\temp\testB.txt";
try
{
using (var streamOutA = new StreamWriter(pathA))
using (var streamOutB = new StreamWriter(pathB))
using (var streamReader = new StreamReader(path))
{
while (streamReader.Peek() >= 0)
{
string line = streamReader.ReadLine();
if ((line.Count(c => c == '|') > 60))
{
streamOutA.WriteLine(line);
}
else
{
streamOutB.WriteLine(line);
}
}
}
}
catch (Exception e)
{
Console.WriteLine("File Exception thrown {0} ", e.Message);
}