gpt4 book ai didi

Java:nio WatchService - 无法检测到第二个文件的创建。为什么?

转载 作者:行者123 更新时间:2023-11-30 07:31:12 24 4
gpt4 key购买 nike

我正在尝试检测文件夹中的文件创建,但 WatchService 无法检测到第二个文件创建。它停留在 key = watcher.take()。知道为什么吗?我正在使用 Ubuntu 11.04 x86。

private void watchWorkingDirectory() {
try {
WatchService watcher = FileSystems.getDefault().newWatchService();
WatchKey key;
key = Paths.get(tailSource, "").register(watcher, ENTRY_CREATE);

for (;;) {

System.err.println("Watching current working directory...............................................");
// wait for key to be signalled
key = watcher.take();

System.out.println("Event detected:");


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

if (kind == ENTRY_CREATE) {
tailSource = event.context().toString();
System.out.println(tailSource);
File file = new File(tailSource);
BufferedReader br = new BufferedReader(new FileReader(file));
System.out.println(br.readLine());
tailer = new Tailer(file, tListener, 1, false);
(new Thread(tailer)).start();
}
}
}

} catch (IOException | InterruptedException e) {
System.out.println(e.getMessage());
}
}

最佳答案

关于Java:nio WatchService - 无法检测到第二个文件的创建。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7413817/

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