是否可以使用java代码监控文件夹? [英] is it possible to monitor folder using java code?

查看:172
本文介绍了是否可以使用java代码监控文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有人知道如何使用java监控文件夹?或者任何人都可以给我一个指示,我怎么能开始这个。这是我的想法。


is there anyone know how to monitor a folder using java? or anyone could gave me a point that how could i start this. here's my thought about it.


启动一个线程来扫描文件夹的更改,这可能是创建,删除,更新此文件夹中的文件或其他事情发生,就像上次更新一样。

但在这种情况下,你必须控制线程循环。如果这个线程循环控制不好,那么这将浪费cpu并可能导致致命的问题。

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.

或者,是有没有框架或一些演示代码来做到这一点?希望我们能找到更好的方法来做到这一点。非常感谢你。

or,is there any framework or some demo code to do this ? hope we could find a better way to do this. thank u very much.

推荐答案

你的想法还不错。

要监视java中的文件夹,通常的方法是使用循环在单独的线程中控制它。
为了节省CPU,您可以使用速度(每n秒检查一次)。

To monitor a folder in java, the usual way is to control it in a separate thread using a loop. In order to save CPU, you can use a tempo (check every n seconds).

然后,要通知此线程的更改,您可以使用观察者设计模式

Then, to notify a change from this thread, you can use the "Observer" design pattern.

这篇关于是否可以使用java代码监控文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆