Java: detecting changes in a file (polling files?)

What is the most efficient way to detect file modification in Java?

I read about file polling, but the polling approach has a number of drawbacks that become apparent as the number of files viewed increases. I prefer not to use polling, but a number of solutions on the Internet seem to indicate polling files as the recommended method. I was hoping that maybe the OS-related callback solution for updating the file system is this possible on both Linux and Windows?

Given that I currently only intend to “watch” 2 files, I am still open to solving file polling. Ideally, I hope that polling files will not have a big impact on performance.

I know Java 7 WatchService, but since 7 is not officially released, I hesitate to use this approach or JNI libraries.

Many thanks!

EDIT: File polling seems to be the way to go. If someone could recommend the safest / most efficient way to implement file polling, that would be great.

+3
source share
2 answers

There is no Java solution for the OS to notify you of a file change. This may be possible using native calls, but each OS will be different in this regard, and it, of course, is not supported in Java. JNI will require a lot of custom coding - if possible.

, , . , . , .

+1

Source: https://habr.com/ru/post/1768672/


All Articles