Package name.pachler.nio.file
Class WatchService
- java.lang.Object
-
- name.pachler.nio.file.WatchService
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
PathWatchService
public abstract class WatchService extends java.lang.Object implements java.io.CloseableA service that provides monitoring forWatchables, reporting changes on these objects (in the case of jpathwatch, these arePathinstances). To utilise file monitoring, a program needs to acquire a watch service first, like this:
WatchService ws = FileSystems.getDefault().newWatchService();A path needs to be constructed for the directory to be watched, by simply using thePathsclass:
// assuming we want to watch the '/tmp' directory on a Unix system. Path path = Paths.get("/tmp");We can now register the path with the watch service. In this case, we want to watch for files created under the /tmp directory, so we register usingPath.register(name.pachler.nio.file.WatchService, name.pachler.nio.file.WatchEvent.Kind<?>...)with theStandardWatchEventKind.ENTRY_CREATEevent kind and no modifiers, like this:
WatchKey key = path.register(ws, ENTRY_CREATE);Note that we receive aWatchKeynow, which represents the registration of our path with the watch service. The key is also used subsequently to retreive events. We'll now wait for an event to occur, like this:
// this will block until an event has occurred, or the WatchService is closed. WatchKey k = ws.take();
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedWatchService()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract voidclose()abstract WatchKeypoll()abstract WatchKeypoll(long timeout, java.util.concurrent.TimeUnit unit)abstract WatchKeytake()
-
-
-
Method Detail
-
close
public abstract void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
poll
public abstract WatchKey poll() throws java.lang.InterruptedException, ClosedWatchServiceException
- Throws:
java.lang.InterruptedExceptionClosedWatchServiceException
-
poll
public abstract WatchKey poll(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, ClosedWatchServiceException
- Throws:
java.lang.InterruptedExceptionClosedWatchServiceException
-
take
public abstract WatchKey take() throws java.lang.InterruptedException, ClosedWatchServiceException
- Throws:
java.lang.InterruptedExceptionClosedWatchServiceException
-
-