Java:WatchService:File.Lastmodified返回0
我正在尝试编写一个基本目录监视器,用于打印文件是创建、更改还是删除的。但我在显示每个文件的“lastModified”时间时遇到问题。请参阅下面的完整代码:Java:WatchService:File.Lastmodified返回0,java,Java,我正在尝试编写一个基本目录监视器,用于打印文件是创建、更改还是删除的。但我在显示每个文件的“lastModified”时间时遇到问题。请参阅下面的完整代码: public static void main(String[] args) throws IOException, InterruptedException { // TODO Auto-generated method stub WatchService watcher = FileSystems.getDefault
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;
}
}
}
publicstaticvoidmain(String[]args)抛出IOException、interruptedeexception{
//TODO自动生成的方法存根
WatchService watcher=FileSystems.getDefault().newWatchService();
Path dir=Path.get(“C:\\Users\\User1\\Desktop\\test”);
WatchKey key=dir.寄存器(watcher,
StandardWatchEventTypes.ENTRY\u创建,
StandardWatchEventTypes.ENTRY\u删除,
StandardWatchEventTypes.ENTRY\u修改
);
对于(;;){
WatchKey key2=watcher.take();
for(WatchEvent事件:key.pollEvents()){
WatchEvent.Kind-Kind=event.Kind();
WatchEvent ev=(WatchEvent)事件;
路径文件名=ev.context();
File fullFilename=filename.toFile();
System.out.println(“事件:|“+kind+”|文件名:+fullFilename.getName()+”|时间:+fullFilename.lastModified());
if(fullFilename.exists()){
System.out.println(fullFilename.getName()+“-Exists”);
}
否则{
System.out.println(“fullFileName不存在”);
}
}
布尔有效值=key.reset();
如果(!有效){
打破
}
}
}
lastModified
方法返回0。我试过测试fileFullname
对象是否确实存在,但由于某种原因它并不存在。但是,当您使用fileFullname.getName()
时,它会很好地返回文件名
我做错了什么?
我感谢你的帮助,并提前感谢你 您需要根据监视的目录解析文件名 您无法获取
lastModified
值,因为事件的上下文文件系统没有您监视的目录作为defaultDirectory
,defaultDirectory
是应用程序运行时的目录,如果您检查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());
}
}
}
导入java.io.File;
导入java.io.IOException;
导入java.nio.file.FileSystems;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.nio.file.StandardWatchEventTypes;
导入java.nio.file.WatchEvent;
导入java.nio.file.WatchKey;
导入java.nio.file.WatchService;
公共类代码段{
公共静态void main(字符串[]args)引发IOException、InterruptedException{
Path pathtwatch=FileSystems.getDefault().getPath(“C:\\tmp\\test”);
try(WatchService WatchService=pathtwatch.getFileSystem().newWatchService()){
Path dir=Path.get(“C:\\tmp\\test”);
目录注册(watchService、StandardWatchEventTypes.ENTRY\u MODIFY、StandardWatchEventTypes.ENTRY\u CREATE、StandardWatchEventTypes.ENTRY\u DELETE);
WatchKey=watchService.take();
做{
for(最终WatchEvent事件:key.pollEvents()){
路径名=(路径)event.context();
文件名=dir.resolve(name).toFile();
System.out.println(dir+“:“+event.kind()+”:“+event.context()+”,Modified:“+filename.lastModified());
}
}while(key.reset());
}
}
}
更多信息请访问
我希望这对您有所帮助。您需要根据监视的目录解析文件名 您无法获取
lastModified
值,因为事件的上下文文件系统没有您监视的目录作为defaultDirectory
,defaultDirectory
是应用程序运行时的目录,如果您检查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());
}
}
}
导入java.io.File;
导入java.io.IOException;
导入java.nio.file.FileSystems;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.nio.file.StandardWatchEventTypes;
导入java.nio.file.WatchEvent;
导入java.nio.file.WatchKey;
导入java.nio.file.WatchService;
公共类代码段{
公共静态void main(字符串[]args)引发IOException、InterruptedException{
Path pathtwatch=FileSystems.getDefault().getPath(“C:\\tmp\\test”);
try(WatchService WatchService=pathtwatch.getFileSystem().newWatchService()){
Path dir=Path.get(“C:\\tmp\\test”);
目录注册(watchService、StandardWatchEventTypes.ENTRY\u MODIFY、StandardWatchEventTypes.ENTRY\u CREATE、StandardWatchEventTypes.ENTRY\u DELETE);
WatchKey=watchService.take();
做{
for(最终WatchEvent事件:key.pollEvents()){
路径名=(路径)event.context();
文件名=dir.resolve(name).toFile();
System.out.println(dir+“:“+event.kind()+”:“+event.context()+”,Modified:“+filename.lastModified());
}
}while(key.reset());
}
}
}
更多信息请访问
我希望这对您有所帮助。这很有帮助,解决了我的问题。非常感谢你的帮助!这很有帮助,解决了我的问题。非常感谢你的帮助!