


WatchKey queuedKey = watchService.take() įor (WatchEvent watchEvent : queuedKey. A simple file watcher library for Java and Kotlin applications. There are several types of events and listeners in Java: each type of event. Note that the return 'key' from above method is not actually used for polling, instead we use WatchService#take() method to return a 'queued' key for efficiency (see next). I am using WatchService to watch change in directory, in particular creation. Once signalled, the WatchKey#reset() method should be called so that the key will return to the 'ready' state.Queued: when a file/folder event occurred, it is 'signalled' and 'queued', and can be retrieved by using WatchKey#pollEvents() method which returns a list of WatchEvent. It can also be in ready state on calling WatchKey#reset() method. Ready state: when WatchKey is created it's in 'ready' state. The return value WatchKey is a token which has internal life-cycle states at difference stages:.We can choose more or less of these constants depending on our needs. The second vararg argument of register() method, StandardWatchEventKinds are WatchEvent.Kind type constants for file/folder create/modify/delete events.WatchKey key = path.register(watchService, The instances of Watchable interface can register() themselves to WatchService, e.g. Registering a directory with the WatchService.Doing so enable us to receive external changes (create/delete/rename/modify) to those directories and the files which reside under them. WatchService watchService = FileSystems.getDefault().newWatchService()Ī single instance of WatchService is used to register multiple directories/subdirectories. Followings are the steps to accomplish that. for (WatchEvent event : key.The provides file/directory change notification mechanism. 4.1ĥ.WatchService watchService = FileSystems.getDefault().newWatchService() ħ.Path path = Paths.get("c:\\directory") ĩ.path.register(watchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE) ġ7. If false is returned, the key is no longer valid and the loop can exit.

After processing the event, we need ready to put it back in the state by calling its reset() method. The Watch Service API is fairly simple to use, and relieves programmers from using third party libraries for files change monitoring. When the event occurs, the key signals and puts it in the observer's queue. Java 7 adds a new feature for its NIO package called Watch Service API which allows applications monitoring directories and files for change events such as creation, deletion and modification. Finally, we implement an infinite loop to wait for incoming events. Next, we register an instance for the folder to be monitored by using the event type that Path wants to focus on.ģ. The first step is to use the newWatchService() method of the class to create a new FileSystem.Ģ. When the service detects an event of interest, it forwards it to the registered process and processes it as needed. When registering, we tell the service that the event types we are interested in are: file creation, file modification or file deletion. It enables us to register folders in the monitoring service. The package provides a file change notification API called Watch Service API. File Watcher can also prevent triggering new actions while an old action is still running. It can also watch multiple folders at the same time (subfolders are handled with a checkbox). html') and ignore all other files that might arrive at the same time. Let's see more information about using Java NIO to view files. File Watcher can process only those files that match a filespec (e.g. We have learned a lot of important points in the Java lesson of yourui.
