gpt4 book ai didi

Java:WatchService:File.Lastmodified 返回 0

转载 作者:行者123 更新时间:2023-11-29 07:50:55 26 4
gpt4 key购买 nike

我正在尝试编写一个基本的目录监视器,用于打印文件是否已创建、更改或删除。但是我无法显示每个文件的“lastModified”时间。请参阅下面的完整代码:

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;
}
}


}

lastModified 方法返回 0。我曾尝试测试 fileFullname 对象是否实际存在,但由于某种原因它不存在。然而,当您使用 fileFullname.getName() 时,它会很好地返回文件名。

我做错了什么?

非常感谢您的帮助,在此先感谢您!

最佳答案

您需要根据监视的目录解析文件名。

您无法获取 lastModified 值,因为事件的上下文文件系统没有将您监视的目录作为 defaultDirectorydefaultDirectory 是应用程序运行的目录,如果您检查 fullFilename 是否存在,您将得到 false

Path filename = ev.context(); 
File fullFilename = filename.toFile();
//fullFilename.exists(); returns false

所以你需要这样解决

Path name = (Path) event.context();
//dir is you watched directory
File filename = dir.resolve(name).toFile();

完整代码

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());
}
}
}

更多信息请访问 docs.oracle.com (Watching a Directory for Changes)

希望对你有帮助

关于Java:WatchService:File.Lastmodified 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21310838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com