This question already has an answer here:
I have been working with some fairly large text files containing about two million lines of text. I don't know the length of the content or the lines in advance, just the number of lines. I have been considering two problems that probably have a similar solution:
- How do I change or add to the nth line, without my computer having to shift everything after the nth line in the computer memory.
- Say I want to delete all the lines in the text file except for the last 100k, every time the number of lines grows beyond two million. How do I keep these last 100k lines, without having to read the entire text file, deleting everything, and rewriting the last 100k?
I suppose it could be solved by putting everything in some database system. However, as that is no option for me, I want to know how to work with text files efficiently.