Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way of reading file

I have a file which contains a certain number of fixed length rows having some numbers. I need to read each row in order to get that number and process them and write to a file. Since I need to read each row, as the number of rows increases it becomes time consuming.

Is there an efficient way of reading each row of the file? I'm using C#.

like image 272
Jay Avatar asked Feb 09 '12 14:02

Jay


1 Answers

File.ReadLines (.NET 4.0+) is probably the most memory efficient way to do this.

It returns an IEnumerable<string> meaning that lines will get read lazily in a streaming fashion.

Previous versions do not have the streaming option available in this manner, but using StreamReader to read line by line would achieve the same.

like image 119
Oded Avatar answered Sep 18 '22 05:09

Oded