How to detect growth in file in java?

179 Views Asked by At

I am reading the content of a File from start to finish (would usually do this with a FileInputStream, but I am not bound to that).

While I am reading the file, other processes might append into that file. That's not a problem, as long as I am reading from the file. But when I am done, I would need to have either one thread per file waiting for input (blocking read) or try to read the file regulary (polling).

I would prefer to use something like select for Sockets, but as far as I found information on that topic FileChannels are always blocking...


What I want to archieve is somewhat like tail -f does on Linux command line. I need to know when there is more data to read...

1

There are 1 best solutions below

0
On

There's no single, reliable way of ensuring a running process with an open file can access data appended to that file. Attributes such as file size tend to be cached - at several levels in most operating systems. On top of the operating system caching, the JVM will also cache file attributes.

And that's all for local file systems on the local host. Caching "hiding" file updates can be even more difficult to handle in networked or distributed file systems.

To access data newly appended to an already-open file, you have to get past all that caching, and how you do that is operating system and often even file system specific, so you can't in general do it in Java - even if the JVM weren't caching attributes itself, which it does do.

The only method in Java that I've found to work reliably is to close the file and reopen it. You can write some JNI code to get around any JVM attribute caching and perhaps even the operating system caching to see if the file has grown, so you can find out if you have to close and reopen the file.