user1000258 user1000258 - 1 year ago 84
Java Question

java.nio.file.WatchEvent gives me only relative path. How can I get the absolute path of the modified file?

I am using Java 7, java.nio.file.WatchEvent along with the WatchService. After registering, when I poll for ENTRY_MODIFY events, i cannot get to the absolute path of the file for the event. Is there any way to get to the absolute path of the file from WatchEvent object?

Answer Source

You need to get the parent directory from the WatchKey to resolve the full path

WatchKey key;
WatchEvent<Path> event;

Path dir = (Path)key.watchable();
Path fullPath = dir.resolve(event.context());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download