首页 > 解决方案 > 在 JAVA watchservice api 中,我如何定位所有驱动器

问题描述

我可以让手表服务在 JAVA 中工作,但我想让它在所有驱动器和文件夹中工作,而不是针对一个驱动器/文件夹。

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

/**
 *
 * @author Admin
 */
public class NewClass2 {
    public static void main(String[] args) throws IOException {
       WatchService watchService = FileSystems.getDefault().newWatchService();

        Path directory = Paths.get("C:\\");

        WatchKey watchKey = directory.register(watchService, 
                StandardWatchEventKinds.ENTRY_CREATE, 
                StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY);

        while (true) {
            for (WatchEvent<?> event : watchKey.pollEvents()) {
                System.out.println(event.kind());
                Path file = directory.resolve((Path) event.context());
                System.out.println(file + " was last modified at " + file.toFile().lastModified());
            }
        }
    }
    }

标签: javawatchservice

解决方案


推荐阅读