I have a 1 GB text file which I need to read line by line. What is the best and fastest way to do this?
private void ReadTxtFile()
{
string filePath = string.Empty;
filePath = openFileDialog1.FileName;
if (string.IsNullOrEmpty(filePath))
{
using (StreamReader sr = new StreamReader(filePath))
{
String line;
while ((line = sr.ReadLine()) != null)
{
FormatData(line);
}
}
}
}
In FormatData()
I check the starting word of line which must be matched with a word and based on that increment an integer variable.
void FormatData(string line)
{
if (line.StartWith(word))
{
globalIntVariable++;
}
}
FormatData
(or a simplified version), just in case. – BilliotFormatData
if you want a fast solution, you be best formatting the data in a separate thread from that reading the data. – DipteranglobalIntVariable
. Given the implementation ofFormatData
, is it important that the lines are indeed read in order? If not reading multiple larger chunks of data and concurrently aggregating the global variable will be more efficient. – Dipteran