【问题标题】:Detect if the directory is modified in java检测目录是否在java中被修改
【发布时间】:2013-10-27 09:34:52
【问题描述】:

我有一个要求,我需要验证我用来由我的 java 应用程序创建的目录的修改。

用户可以通过创建文件并删除文件或修改来修改目录,因此我只想验证特定目录是否已修改。

我的解决方案:我需要一个包含最后修改时间的哈希,并且每次我必须通过我的哈希匹配目录的最后修改时间。

但是我的技术人员反对有很多技巧和库,以便用户可以修改其上次修改时间。

所以请告诉我,请替换它。

【问题讨论】:

  • 查看 Java 7 的 WatchService

标签: java windows validation directory


【解决方案1】:

使用 java 7 手表服务 api

for (;;) {

    WatchKey key;
    try {
        key = watcher.take();
    } catch (InterruptedException x) {
        return;
    }

    for (WatchEvent<?> event: key.pollEvents()) {
        WatchEvent.Kind<?> kind = event.kind();

        if (kind == OVERFLOW) {
            continue;
        }

        WatchEvent<Path> ev = (WatchEvent<Path>)event;
        Path filename = ev.context();
       try {
            Path child = dir.resolve(filename);
            if (!Files.probeContentType(child).equals("text/plain")) {
                System.err.format("New file '%s'" +
                    " is not a plain text file.%n", filename);
                continue;
            }
        } catch (IOException x) {
            System.err.println(x);
            continue;
        }

        System.out.format("Emailing file %s%n", filename);
     }

    boolean valid = key.reset();
    if (!valid) {
        break;
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-10
    • 2014-04-29
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    相关资源
    最近更新 更多