Java监视多个文件夹

时间:2017-04-25 14:09:12

标签: java watchservice

我需要监控多个文件夹,我想知道哪个文件夹已收到  执行特定治疗的事件。我试过这段代码。

connection.end()

是否可以这样做,或者我必须为每个文件夹创建watchService。

感谢。

1 个答案:

答案 0 :(得分:1)

WatchService.take()将为每个观看的目录返回不同的监视键。你可以从WatchKey::watchable()方法获得观看的目录。

因此,通过检查WatchKey::watchable()的返回值,您将知道此事件的目录。

  

WatchKey通常是不变的吗?

没有。 WatchKey不是常数。您可以通过致电WatchKey watchKey = watchService.take();来获取它。您已经在已发布的代码中完成了它。

  

我怎么能说这是opexFolder的密钥或docupostFolder的密钥

只需致电Path directory = WatchKey::watchable()

这是一个简单的示例应用程序:

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public final class WatchServiceExample {

    public static void main(String[] args) throws Exception {
        //We'll use this watch service to monitor all directories we are interested in
        final WatchService watchService = FileSystems.getDefault().newWatchService();

        //We'll use this directory for the test in order not to create junk in the system
        final Path tempDirectory = Files.createTempDirectory("watch-service-example");
        System.out.println("Created temporary directory: " + tempDirectory.toAbsolutePath());

        for (int i = 0; i < 10; i++) {
            final Path watchedDir = Files.createDirectory(tempDirectory.resolve("watched_" + i));
            System.out.println("Created watched directory: " + watchedDir.toAbsolutePath());
            watchedDir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
        }

        System.out.println("Initialization complete. When you create an entry in the watch dirs you'll be notified.");
        while (true) {
            final WatchKey watchKey = watchService.take();
            final Watchable watchable = watchKey.watchable();

            //Since we are registering only paths in teh watch service, the watchables must be paths
            if (!(watchable instanceof Path)) {
                throw new AssertionError("The watchable should have been a Path");
            }

            final Path directory = (Path) watchable;
            System.out.println("Processing events for watched directory: " + directory);

            for (WatchEvent<?> event : watchKey.pollEvents()) {
                System.out.println("Received event '"
                  + event.kind()
                  + "' for entry '"
                  + event.context()
                  + "' in watched directory '"
                  + directory + "'"
                );
            }

            if (!watchKey.reset()) {
                System.out.println("Failed to reset watch key: will not process more events");
                break;
            }
        }

        //Lets clean up after ourselves
        Files.walkFileTree(tempDirectory, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

示例应用程序的输出:

Created temporary directory: /tmp/watch-service-example4292865635932187600
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_0
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_1
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_2
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_3
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_4
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_5
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_6
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_7
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_8
Created watched directory: /tmp/watch-service-example4292865635932187600/watched_9
Initialization complete. When you create an entry in the watch dirs you'll be notified.
Processing events for watched directory: /tmp/watch-service-example4292865635932187600/watched_5
Received event 'ENTRY_CREATE' for entry 'test' in watched directory '/tmp/watch-service-example4292865635932187600/watched_5'
Processing events for watched directory: /tmp/watch-service-example4292865635932187600/watched_8
Received event 'ENTRY_CREATE' for entry 'test' in watched directory '/tmp/watch-service-example4292865635932187600/watched_8'