处理目录中传入的文件
我想制作一个Java程序,连续监视目录中是否有新文件,
如果有新文件到达,则对其进行处理,然后将其删除。
最好的方法是什么?
I want to make a Java program that monitors a directory continuously for a new file,
and if a new file arrives, process it and then delete it.
What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查新文件、处理它们、冲洗并重复:
Check for new files, process them, rinse, and repeat:
JDK 7 包含 java.nio.file.WatchService 类,该类允许您在文件系统发生更改时收到通知,而不是持续轮询。目前它也可在 OpenJDK 中使用:
JDK 7 功能
OpenJDK WatchService 教程
JDK 7 includes the
java.nio.file.WatchService
class that allows you to be notified of filesystem changes when they happen rather than continuously poll. It is currently available in OpenJDK as well:JDK 7 features
OpenJDK WatchService tutorial