Java检测文件系统中的更改 [英] Java detect changes in filesystem

查看:230
本文介绍了Java检测文件系统中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹,在这个文件夹中连续不断地转储新文件。在Java中,检测文件系统(即文件被转储的指定文件夹)更改的最佳方法是什么?文件到一个队列数据结构,以便我可以顺序处理每个传入的文件。

我知道File类中的listFiles()函数,但是使用这个我只能获取即时可用的文件。当然,我可以连续轮询文件夹,并使用线程获取文件列表。但这是最好的方法,或者有更好的方法来完成这个。

解决方案

不断轮询是Java中的一种方式 - 尽管不要频繁轮询,如果目录中包含大量条目,那么操作可能相当繁重。 p>

JDK 7将具有特定的API来执行此操作 java.nio.file.WatchFile


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.

解决方案

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

这篇关于Java检测文件系统中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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