Is filesystem polling only option? Or is there support for FSEvents
(OSX), ReadDirectoryChangesW
(Windows)?
2 Answers
You can use a WatchService
:
The implementation that observes events from the file system is intended to map directly on to the native file event notification facility where available, or to use a primitive mechanism, such as polling, when a native facility is not available
You can also have a look at this tutorial which confirms that point
Most file system implementations have native support for file change notification. The Watch Service API takes advantage of this support where available. However, when a file system does not support this mechanism, the Watch Service will poll the file system, waiting for events

- 321,522
- 82
- 660
- 783
-
Any way to check if the native OS APIs are really used? If I run that example and modify a file in a folder that's being watched it takes about 4-5 seconds to detect the change whereas with a C program using FSEvents it detects the change instantly. – daniels Oct 27 '15 at 23:53
-
I don't know to be honest... You may find more info in that thread: http://mail.openjdk.java.net/pipermail/nio-dev/2014-August/002691.html (and the answer seems to be that it uses polling in OSX) – assylias Oct 28 '15 at 08:12
-
Ok, so no native support for OSX yet in the JDK/JRE. – daniels Oct 28 '15 at 11:46
-
2020 can confirm. still lacking for osx – mjs Jan 14 '20 at 22:37
WatchService is meant to provide this functionality, and there are implementations that use native events included in the JRE for most major OSes except for macOS (as of Feb. 2020).
For macOS, gmethvin/directory-watcher includes an open source implementation of WatchService that uses native events that can be used.

- 2,204
- 1
- 19
- 18