Java检测文件系统的变化
我有一个文件夹,其中不断转储新文件。在 Java 中,检测文件系统(即转储文件的指定文件夹)中的更改并将新到达的文件添加到队列数据结构,以便我可以顺序处理每个传入的文件。
我知道 File 类中的 listFiles() 函数,但使用它我只能获取即时可用的文件。当然,我可以连续轮询文件夹并使用线程获取其中的文件列表。但是这是最好的方法还是有更好的方法来完成此任务。
I have a folder in which continuously new files are being dumped.In Java,what is the best way to detect changes in file-system (ie. a specified folder in which the files are being dumped) and add the newly arrived files to a queue data structure so that i can sequentially process each incoming file.
I'm aware of listFiles() function in the File class but using this I can only get files that are available at an instant of time. Of course I can continuously poll the folder and get the list of files in it using a thread.But is this the best way or is there a better way to accomplish this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,Java 中采用的方法是持续轮询 - 尽管不要轮询太频繁,但如果目录包含大量条目,这可能是一项相当繁重的操作。
JDK 7 将有一个特定的 API 来执行此操作 java .nio.file.WatchFile
Continuously polling is the way to do it in Java as of now - though don't poll too often, it can be quite a heavy operation if the directory contains lots of entries.
JDK 7 will have a specific API for doing just this java.nio.file.WatchFile
不幸的是,在 JDK7 发布之前,没有标准的方法可以做到这一点。
但是互联网上有一些库可以使用不同操作系统的本机功能来执行此操作。
我看过的图书馆是
jPoller 和 j通知
但最终我只是简单地轮询了当我不得不这样做时我感兴趣的目录。
There is unfortunately no standard way to do this until JDK7 comes out.
But there are some libraries available on the internet which use the native functions of different operating systems to do this.
The libraries which I have looked at are
jPoller and jNotify
But in the end I ended simply polling the directory which was interesting for me when I had to do that.