JDK7 java.nio.file.WatchService - not flexible enough?
843810Jan 17 2010 — edited Jan 18 2010Hi,
is it indeed impossible to add/remove watched directories when needed by using a single WatchService?
My case:
- I'm creating a file browser using Java 7 (jdk build 79) The user (the file browser) navigates to a dir and starts listening to file-related events (like modified, created, deleted) on this dir. Once the user navigates to another dir the file browser has to stop listening to the old dir and start listening to the new one for events.
Impossible because:
- Stopping watching the folder thru WatchKey.cancel() doesn't work when you haven't received any notifications from the corresponding directory yet and the user navigates to another folder. In this case the WatchKey is null and I can't use it to stop listening to the (already old) folder by calling WatchKey.cancel().
In this case the only thing left is closing the WatchService altogether and starting a new WatchService each time. Is this what a developer is supposed to do?
And obviously the third way to stop listening for a directory (the WatchService stops listening to deleted directories) - doesn't cut it either.
Edited: corrected typo "WatchKeyEvent" with "WatchKey"