Java parallel read / write file

I need to read a text file from my Java application.

The file contains many lines, and this file is updated every X minutes from an external unknown application that adds new lines to the file.

I need to read all the lines from a file, and then delete all the records that I just read.

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?

This file is located in the Samba public folder, so I use jCIFS to read / write the file and the Java BufferedReader class.

early

+6
source share
3 answers

I do not know the perfect solution to your problem, but I would solve it differently:

  • rename the file (give it a unique name with a timestamp)
  • The appender job will automatically recreate it.
  • process your files with a timestamp (there is no need to delete them, keep them in place, to later check what happened)
+2
source

The problem is that we do not know how an external application writes and / or reuses this file. This can be a problem if you delete rows while an external application uses a counter to work correctly ...

There is no good solution if you do not know how another application works.

+1
source

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.

0
source

All Articles