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++; } }
Solution 1: Download a Dedicated Large File Viewer If all you need to do is read the large file, you can download a dedicated large file viewer such as the Large Text File Viewer. Such tools will open large text files with ease.
The best way to view extremely large text files is to use… a text editor. Not just any text editor, but the tools meant for writing code. Such apps can usually handle large files without a hitch and are free. Large Text File Viewer is probably the simplest of these applications.
If you are using .NET 4.0, try MemoryMappedFile which is a designed class for this scenario.
You can use StreamReader.ReadLine
otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With