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.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How can I create this custom Bottom Navigation on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
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
offset
andlength
of 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 aRandomAccessFile
to 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.
You can delete a line by copying the rest of the data in the file, then flushing the file and finally writing the copied data.Thr following code searches for the string to be deleted and skips the code to copy in a stringBuider . The contents of stringBuilder are copied to the same file after flushing
Try storing file into a String buffer replace what you intend to replace, then replace the contents of the file entirely.