Java I/O and NIO.2

NIO.2 cookbook, Part 3

Advanced recipes for file copying, finding files, and watching directories with NIO.2

Java I/O and NIO.2

Show More
1 2 3 Page 3
Page 3 of 3

A watch key has state. It is in the ready state when initially created and in the signalled state when an event is detected (the watch key is then queued for retrieval by poll() or take()). Events detected while the key is in the signalled state are queued but don't cause the key to be requeued for retrieval from the watch service. Events are retrieved by invoking WatchKey's List<WatchEvent<?>> pollEvents() method.

For each WatchEvent object stored in the returned java.util.List object, WatchEvent.Kind<T> kind() returns the kind of event that has been returned. If the event kind is OVERFLOW, events have been lost (because the file system is generating them too quickly to keep up). This condition is reported and the loop is continued (there is no other meaningful information to be obtained and reported).

For every other event kind, the WatchEvent<?> object is cast to WatchEvent<Path> (the resulting unchecked warning message is suppressed) in order to invoke WatchEvent<Path>'s Path context() method, which returns the event's context (the relative path between the directory registered with the watch service and the entry that is created, deleted, or modified). The event kind and context are subsequently output.

Finally, the watch key must be reset to the ready state, by invoking WatchKey's boolean reset() method. reset() returns true when the watch key is valid and has been reset. It returns false when the watch key could not be reset because it's no longer valid (perhaps because the watch service has been closed). (This step is very important. Fail to invoke reset() and this key will not receive any further events.)

Building and running Watch

Execute the following command to compile Watch.java:

javac Watch.java

Assuming successful compilation, execute the following command to execute Watch and monitor the current directory, which will contain Watch.java and Watch.class:

java Watch

Using a separate command window, change to the same directory and output the following instructions (I'm assuming a Windows platform):

md test
rd test
copy Watch.java Watch.bak
erase Watch.bak

You should observe the following output, which indicates that directory test has been created and deleted, and that file Watch.bak has been created, modified, and deleted:

ENTRY_CREATE: test
ENTRY_DELETE: test
ENTRY_CREATE: Watch.bak
ENTRY_MODIFY: Watch.bak
ENTRY_MODIFY: Watch.bak
ENTRY_DELETE: Watch.bak

What's next?

JDK 8u40 introduced several enhancements to JavaFX 8: support for accessibility, new dialog boxes, a new spinner control, and a text-formatting capability. Next time, I'll introduce you to each of these enhancements.

download
Get the source code for this post's applications. Created by Jeff Friesen for JavaWorld

The following software was used to develop the post's code:

  • 64-bit JDK 7u6

The post's code was tested on the following platform(s):

  • JVM on 64-bit Windows 7 SP1
Related:
1 2 3 Page 3
Page 3 of 3