轮询文件以进行更改? [英] Poll a file for change?

查看:110
本文介绍了轮询文件以进行更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个模式,我希望轮询一个文件的变化(当它上次写)。当文件确实从以前的值改变,我希望执行一些功能。

 (poll-for-changes file-str on-change-fx current-value)

其中


  1. code> file-str 只是一个指定文件位置的字符串

  2. on-change-fx 是当 file-str 的文件更改时应调用的函数。让我们说, on-change-fx 应该使用指向 file-str 的File对象作为参数。

  3. current-value 文件的当前值(以毫秒为单位)。您可以设置为0,以保证此函数至少运行一次,或者设置为实际值,以便在实际检测到更改时只运行此函数。

我只是希望这个函数以最清晰,最简洁,Clojurist的方式实现。谢谢。

解决方案

如果你想轮询一个目录或文件并执行它,我认为 watchtower 相当不错。



Java 7有一个WatchService,它使用文件系统事件来响应更改。在这种情况下,您不会轮询,但阻止未来的文件事件。我不认为在Clojure有任何项目利用这一点,虽然我花了一些时间与它一起写一个小图书馆。其来源在这里 p>

我不声称我的库甚至是完整的,但它确实使用Java 7服务,所以你可以使用它为自己的项目的灵感。


It is often a pattern that I wish to poll a file for changes (when it was last written). When the file does change from its previous value, I wish to execute some function. Something of the form.

(poll-for-changes file-str on-change-fx current-value)

where

  1. file-str is just a string that specifies the files location
  2. on-change-fx is the function that should be called when the file at file-str changes. Let us say that the on-change-fx should take the File object pointing to file-str as a argument.
  3. current-value the current value of the file in milliseconds. You might set to 0 to guarantee that this function will run at least once, or to the actual value to only run this function when you actually detect a change.

I would just like this function implemented in the clearest, most concise, Clojurist way possible. Thank you.

解决方案

If you're looking to poll a directory or files and act on it, I think watchtower is pretty good to look at.

Java 7 has a WatchService, which uses file system events to react to changes. In this case, you don't poll at all, but block on a future file event. I don't think there are any projects in Clojure that are out there leveraging that, although I spent some time toying with it to write a small library. The source for it is here

I don't claim my library is even complete, but it does use the Java 7 service, so you could use that for inspiration on your own project.

这篇关于轮询文件以进行更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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