daniels
daniels

Reputation: 19233

Does Java have native OS file system events monitoring support?

Is filesystem polling only option? Or is there support for FSEvents (OSX), ReadDirectoryChangesW (Windows)?

Upvotes: 7

Views: 1222

Answers (2)

Chris H.
Chris H.

Reputation: 2273

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.

Upvotes: 0

assylias
assylias

Reputation: 328835

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

Upvotes: 7

Related Questions