public interface PathWatchService extends WatchService
Path
objects for changes.
A PathWatchService
is similar to a WatchService
except that
a PathWatchService
must be started, it only watches Path
instances (not Watchable
instances), and instead of calling one of
the register
methods on a Watchable
, we call one of the
register
methods on this interface.
To use this service, create an instance, register directories to watch via
the register
methods, and start it via the start
method.
Registering directories to watch can be performed before and after starting
the service. Retrieve changes via the poll
or take
methods,
and close the service when done via the close
method.
Example:
PathWatchService s = newPathWatchService(); s.register(FileSystems.getDefault().getPath("/home/luke"), StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); s.start(); for (;;) { WatchKey k = s.take(); ... }
Modifier and Type | Method and Description |
---|---|
WatchKey |
register(Path dir,
WatchEvent.Kind<?>... kinds)
Registers the specified directory, using the specified
WatchEvent
kinds and no WatchEvent modifiers, with this watch service. |
WatchKey |
register(Path dir,
WatchEvent.Kind<?>[] kinds,
WatchEvent.Modifier... modifiers)
Registers the specified directory, using the specified
WatchEvent
kinds and modifiers, with this watch service. |
void |
start()
Starts this service.
|
close, poll, poll, take
void start()
ClosedWatchServiceException
- if closedIllegalStateException
- if already startedWatchKey register(Path dir, WatchEvent.Kind<?>... kinds) throws IOException
WatchEvent
kinds and no WatchEvent
modifiers, with this watch service.
This method is intended to be similar in behavior to
Path.register(WatchService, WatchEvent.Kind[])
.
dir
- directory to watch; must not be a symbolic linkkinds
- events to register forClosedWatchServiceException
- if this watch service is closedNotDirectoryException
- if dir
is not a directoryIOException
- if an I/O error occursWatchKey register(Path dir, WatchEvent.Kind<?>[] kinds, WatchEvent.Modifier... modifiers) throws IOException
WatchEvent
kinds and modifiers, with this watch service.
This method is intended to be similar in behavior to
Path.register(WatchService, WatchEvent.Kind[], WatchEvent.Modifier[])
.
dir
- directory to watch; must not be a symbolic linkkinds
- events to register formodifiers
- modifiers modifying how dir
is registeredClosedWatchServiceException
- if this watch service is closedNotDirectoryException
- if dir
is not a directoryIOException
- if an I/O error occurs