Can I allow me to read a file line by line, deleting every line I read and at the same time letting the external application add other lines to the file?
Yes, you can open the same file for reading and writing from several processes. On Linux, for example, you get two separate file descriptors for the same file. To write files under the size of PIPE_BUF, or 4096 bytes in Linux, we can safely assume that the operations are atomic, that is, the kernel processes locks and unlocks to prevent race conditions.
Assuming process A writes to a file, he opened it as APPEND, then each time process A tells write() to the kernel, it will first look for the file size (end of file). This means that you can safely delete data in a file from process B while it is done between the write operations of process A. And while the write operations from process A do not exceed PIPE_BUF, Linux ensures that they are atomic, i.e. process A can spam write operations, and process B can permanently delete / write data, and there will be no bizarre behavior.
Java provides you with implemented file locks . But it’s important to understand that this is only “advisory” and not “mandatory”. Java does not apply a restriction, both processes must perform a check to ensure that the other process contains a lock.
source share