I mean , I want to delete line from my text on android. How can I delete?
I do not want to read one txt and create another with removing line. I want to delete line from my existing txt.
thanks.
I mean , I want to delete line from my text on android. How
Share
This is a pretty tricky problem, despite it looking a trivial one. In case of variable lines length, maybe your only option is reading the file line by line to indentify
offsetandlengthof the target line. Then copying the following portion of the file starting atoffset, eventually truncating the file lenght to its original size minus the the target line’s length. I use aRandomAccessFileto access the internal pointer and also read by lines.This program requires two command line arguments:
args[0]is the filenameargs[1]is the target line number (1-based: first line is #1)Here is the full code, including a JUnit test case. The advantage of using this solution is that it should be fully scalable with respect to memory, ie since it uses a fixed buffer, its memory requirements are predictable and don’t change according to the input file size.