Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a large (1 GB) txt file in .NET?

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++;     } } 
like image 731
Jeevan Bhatt Avatar asked Nov 25 '10 04:11

Jeevan Bhatt


People also ask

How can I open a text file larger than 1gb?

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.

How can I open a very large text file?

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.


1 Answers

If you are using .NET 4.0, try MemoryMappedFile which is a designed class for this scenario.

You can use StreamReader.ReadLine otherwise.

like image 51
TalentTuner Avatar answered Oct 14 '22 02:10

TalentTuner