gpt4 book ai didi

java - WatchService 用于创建文件的监视特定目录

转载 作者:行者123 更新时间:2023-12-01 05:31:58 26 4
gpt4 key购买 nike

public static void main (String args[]) throws Exception {
Path _directotyToWatch = Paths.get(args[0]);
WatchService watcherSvc = FileSystems.getDefault().newWatchService();
WatchKey watchKey = _directotyToWatch.register(watcherSvc, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
while (true) {
watchKey=watcherSvc.take();
for (WatchEvent<?> event: watchKey.pollEvents()) {
WatchEvent<Path> watchEvent = castEvent(event);
System.out.println(event.kind().name().toString() + " " + _directotyToWatch.resolve(watchEvent.context()));
watchKey.reset();
}
}
}

在上面的示例中,监视目录路径取自控制台参数。我想静态传递目录路径。

尝试过这个 Paths.get("O:\\test");但抛出异常

Exception in thread "main" java.lang.NoClassDefFoundError: java/nio/file/Paths
at JSR203_NIO2_WatchFolder.main(JSR203_NIO2_WatchFolder.java:40)
Caused by: java.lang.ClassNotFoundException: java.nio.file.Paths
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

最佳答案

尝试

Path _directotyToWatch = Paths.get("O:/test"); 

关于java - WatchService 用于创建文件的监视特定目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8830509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com