Jon
Jon

Reputation: 2584

Delete the first line of a text file without reading it

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?

Upvotes: 7

Views: 2889

Answers (1)

Noctis
Noctis

Reputation: 11763

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 ...

Upvotes: 1

Related Questions