Java 7 uses the underlying file system functionalities to watch the file system for changes. Now, we can watch for events like creation, deletion, modification, and get involved with our own actions. For accomplish this task, we need:
• An object implementing the Watchable interface - the Path class is perfect for this job.
• A set of events that we are interested in - we will use StandardWatchEventKind which implements the WatchEvent.Kind<T>.
• An event modifier that qualifies how a Watchable is registered with a WatchService.
• A watcher who watch some watchable – per example, a watcher that watches the File System for changes. The abstract class is java.nio.file.WatchService but we will be using the FileSystem object to create a watcher for the File System.
The below example follows the above scenario:
The FileSystem object and the WatchService can also be created like this:
And the Path (watchable), what we watch, and register it with the WatchService object like this:
本文介绍如何使用Java 7的文件系统监控功能来监听文件系统的变更,包括创建、删除和修改等事件,并通过示例代码展示如何实现这一功能。

7057

被折叠的 条评论
为什么被折叠?



