gpt4 book ai didi

Java如何检测文件变化

转载 作者:行者123 更新时间:2023-12-02 09:50:16 25 4
gpt4 key购买 nike

有没有办法以某种方式触发一个事件,每次文件更改时都会执行某些操作?我的意思是类似的东西(伪代码)

if(DetectedFileModified(pathToFile)){ do stuff }

我知道我可以定期执行此操作,但我可以在没有计时器的情况下以某种方式执行此操作吗?我希望能够在用户更新 txt 文件时执行一些操作(不是通过代码,而是通过打开文件并在其中写入),但我不知道他什么时候会执行此操作。

最佳答案

您需要一个 WatchService

The WatchService API is fairly low level, allowing you to customize it. You can use it as is, or you can choose to create a high-level API on top of this mechanism so that it is suited to your particular needs.

Oracle blog on the same

关于Java如何检测文件变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18241976/

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