Java:WatchService:File.Lastmodified返回0

时间:2014-01-23 14:23:23

标签: java

我正在尝试编写一个基本目录监视器,用于打印是否创建,更改或删除文件。但是我无法显示每个文件的“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()时,它会返回文件名。

我做错了什么?

感谢您的帮助,并提前感谢您!

1 个答案:

答案 0 :(得分:4)

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

您无法获取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)

的更多信息

我希望这会对你有所帮助。