I am trying to write a basic directory monitor that prints whether a file is created, changed, or deleted. But I am having trouble displaying the 'lastModified' time of each file. Please see full code below:
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get("C:\\Users\\User1\\Desktop\\test");
WatchKey key = dir.register(watcher,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY
);
for (;;) {
WatchKey key2 = watcher.take();
for ( WatchEvent<?> event : key.pollEvents() ) {
WatchEvent.Kind<?> kind = event.kind();
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path filename = ev.context();
File fullFilename = filename.toFile();
System.out.println("Event: |"+kind+"| Filename: "+fullFilename.getName()+"|Time: "+fullFilename.lastModified());
if (fullFilename.exists()) {
System.out.println(fullFilename.getName()+" - Exists");
}
else {
System.out.println("fullFileName does not exist");
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
}
The lastModified
method returns 0. I have tried testing whether fileFullname
object actually exists, and for some reason it doesn't. Yet, when you use fileFullname.getName()
it returns the filename just fine.
What am I doing wrong?
I appreciate your help, and thank you in advance!
You need resolve the filename against the watched directory.
You can not get the lastModified
value because the context filesystem of the event does not have your watched directory as defaultDirectory
, the defaultDirectory
is the directory from the app runs, if you check the existence of fullFilename
you will get false
Path filename = ev.context();
File fullFilename = filename.toFile();
//fullFilename.exists(); returns false
so you need resolve it by this way
Path name = (Path) event.context();
//dir is you watched directory
File filename = dir.resolve(name).toFile();
The complete code
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class Snippet {
public static void main(String[] args) throws IOException, InterruptedException {
Path pathToWatch = FileSystems.getDefault().getPath("C:\\tmp\\test");
try (WatchService watchService = pathToWatch.getFileSystem().newWatchService()) {
Path dir = Paths.get("C:\\tmp\\test");
dir.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
WatchKey key = watchService.take();
do {
for (final WatchEvent<?> event : key.pollEvents()) {
Path name = (Path) event.context();
File filename = dir.resolve(name).toFile();
System.out.println(dir + ": " + event.kind() + ": " + event.context() + ", Modified: " + filename.lastModified());
}
} while (key.reset());
}
}
}
More information at docs.oracle.com (Watching a Directory for Changes)
I hope this helps you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.