gpt4 book ai didi

java - 是否可以使用 java 代码监视文件夹?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:48:02 25 4
gpt4 key购买 nike


有谁知道如何使用 java 监视文件夹?或者任何人都可以给我一个观点,我该如何开始。这是我的想法。

start a thread to scan the folder changes,which could be create,delete,update files in this folder or something else happen,like last updated.
but in this case, you have to control thread loop. if this thread loop is not controlled well,then it would be a waste of cpu and may cause a fatal problem.

或者,是否有任何框架或一些演示代码可以执行此操作?希望我们能找到更好的方法来做到这一点。非常感谢你。

最佳答案

JDK7 的 java.nio.file 包有一个 WatchService支持文件更改通知。您可以使用它来监视目录的更改。这在可用的情况下使用 native 设施,否则它使用原始机制,例如轮询。 Here是关于它的帖子。

现在,您可以尝试 jpathwatch ,它是 WatchService 接口(interface)的实现,使用 native OS 功能,而不是轮询。

关于java - 是否可以使用 java 代码监视文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3810790/

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