Compare commits

...

3 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire e188f4cb58 Java NIO
- Cleaned up
2023-09-15 10:42:12 +02:00
Tristan B. Velloza Kildaire 3cc2b32d54 Java NIO
- Switch to using `WatchService`'s `take()` method
2023-09-15 10:36:40 +02:00
Tristan B. Velloza Kildaire 7a7cf62546 Java NIO
- Fixed the path in the `WatchService` example, we now resolve it to the watched bpath `path1`
- Mentioned we need to `reset()` the `WatchKey` after calling `take()` on the `WatchService` such that it re-registers for events
2023-09-15 10:34:38 +02:00
1 changed files with 17 additions and 4 deletions

View File

@ -17,6 +17,7 @@ import java.nio.file.attribute.UserPrincipal;
import java.nio.file.spi.FileSystemProvider;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
/**
@ -112,7 +113,13 @@ public class App
* I sleep here to give me time to modify an directory
* entry in /home/deavmi
*/
Thread.sleep(5000);
// Thread.sleep(5000);
/**
* Alternatively you can call {@link WatchService}'s
* <code>take()</code> method which blocks till an event
*/
eventHandle = watcher.take();
/**
@ -133,10 +140,17 @@ public class App
*/
if(event.kind() == StandardWatchEventKinds.ENTRY_MODIFY)
{
System.out.println("Modified path: "+((Path)event.context()).toAbsolutePath());
System.out.println("Modified path: "+(path1.resolve((Path)event.context())));
}
}
/**
* Because we called <code>take()</code> on the {@link WatchService}
* it has removed it and it won't be watched (the directory the key
* is registered for) until we call <code>reset()</code> below
* which places it back into the watcher for watching.
*/
eventHandle.reset();
}
catch (Exception e)
{
@ -195,6 +209,5 @@ public class App
UserPrincipal user = Files.getOwner(resolvedPath, LinkOption.NOFOLLOW_LINKS);
System.out.println("The owner is: '"+user+"'");
}
}
}