Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete the first line of a text file without reading it

Tags:

c#

file

I'm working with a very large text file (around 70 thousand lines) and I want to remove the top line.

Clearly, loading the entire thing into memory, deleting the top line, then re-writing the whole thing again is inefficient:

var lines = File.ReadLines(accountFileLocation.Text).Skip(1);
File.WriteAllLines("output.txt", lines);

Is there any other way to do it?

like image 854
Jon Avatar asked Nov 14 '13 04:11

Jon


1 Answers

Hehe .... finally I can say Jon Skeet said :)

Jon Skeet said : not really

What you did is one approach. The second will have to open a read stream and a write stream (to a different file), and read line, then write it into the write if you want it (more for a "test lines for validity, than a all but the first line).

So ... seems like you got the right answer ...

like image 104
Noctis Avatar answered Sep 28 '22 08:09

Noctis