如何仅显示文件更改事件Java的一条消息

时间:2019-03-27 15:48:39

标签: java watchservice file-structure

文件结构更改时。对于每个已更改的文件,我都会不断收到println个文件。如何做到在10秒钟内只能得到一个println多项更改

try {
    WatchService watchService = FileSystems.getDefault().newWatchService();
    Path path = new File("C:\\Users\\myuser\\Desktop\\TestFolder").toPath();

    path.register(
        watchService,
        StandardWatchEventKinds.ENTRY_CREATE,
        StandardWatchEventKinds.ENTRY_DELETE,
        StandardWatchEventKinds.ENTRY_MODIFY);

    WatchKey key;

    while ((key = watchService.take()) != null) {
        for (WatchEvent<?> event : key.pollEvents()) {

            System.out.println(
                "Event kind:" + event.kind() + ". File affected: " + event.context() + ".");
            System.out.println("Something changed!");
            Thread.sleep(10000);
            System.out.println("Resuming..");
            break;
        }
        key.reset();
    }
} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

3 个答案:

答案 0 :(得分:0)

好吧,您可以结合使用Registered web context: '/kie-server' from server 'default-server' Registered web context: '/jbpm-casemgmt' from server 'default-server' Registered web context: '/business-central' from server 'default-server' WatchService.poll(long timeout, TimeUnit unit)做类似的事情。

看看这个

Thread.sleep(10000)

可以从此处获取完整代码:Github Code for WatchService 10 seconds example.

答案 1 :(得分:0)

我只需轮询所有键并连接结果即可。

与此类似:

while ((key = watchService.take()) != null) {
    StringJoiner joiner = new StringJoiner(",");
    for (WatchEvent<?> event : key.pollEvents()) {
        joiner.add(event.getContext().toString())        
    }
    key.reset();
    Thread.sleep(10000);

    System.out.println("Resuming ...");
    System.out.println("Something has changed! Affected files:" + joiner.toString())
}

答案 2 :(得分:0)

现在,据我所知,线程处于休眠状态时,您的事件会排队并且will still be processed。我想从编写的内容来看,您想要的是仍然处理这些事件,但是仅在10s之后打印事件的完整列表。在这种情况下,处理事件时的Thread.sleep()不是一个好的解决方案。

您可以做的是为观察者实现一个线程,例如,它将它注册的每个事件添加到变量in a thread-safe way中,供主线程在10秒后访问。因此,在您的主线程中,您将有一个循环,该循环将休眠10秒钟,并且在唤醒后检索此变量,将其打印出来并重置它。

应该是这样的:

private Object lock = new Object(); // defined in your watcher class
private String events = new String() // also defined in your class

thread = new Thread(listeningFunc);
thread.start()

while(True) {
    Thread.sleep(10000);
    syncronized(lock) {
        System.out.println(events);
        events = new String()
    }
}

// With this function defined in your class to run in your new thread
listeningFunc() {

    path.register(
        watchService,
        StandardWatchEventKinds.ENTRY_CREATE,
        StandardWatchEventKinds.ENTRY_DELETE,
        StandardWatchEventKinds.ENTRY_MODIFY);

    WatchKey key;

    while((key = watchService.take()) != null) {
        for (WatchEvent<?> event : key.pollEvents()) {
            syncronized(lock) {
                events += "Event kind:" + event.kind() + ". File affected: " + event.context() + ".\n"
            }
        }
        key.reset()
    }
}