java 如何轮询目录以检查是否添加了新文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7344331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to poll directory to check whether new file is added?
提问by ramesh
I want to poll a directory to check whether new file is added to the directory. If any new file is added I want to read that file.
我想轮询一个目录以检查是否有新文件添加到该目录中。如果添加了任何新文件,我想读取该文件。
Can anybody give me an idea how to do that?
谁能给我一个想法如何做到这一点?
回答by TofuBeer
- Java 7 has a file watcher API
- JNotifywill do it as well.
回答by Jesper
If you are using Java 7, you can use the filesystem watch service (a new feature in Java 7).
如果您使用的是 Java 7,则可以使用文件系统监视服务(Java 7 中的一项新功能)。
See Oracle's tutorialthat explains how to use it.
请参阅说明如何使用它的Oracle 教程。
Otherwise (if you're using an older Java version) you can use a library such as Apache Commons IO. Look at the package org.apache.commons.io.monitor
- it has classes to check for changes in files and directories.
否则(如果您使用的是较旧的 Java 版本)您可以使用诸如Apache Commons IO 之类的库。查看包org.apache.commons.io.monitor
- 它具有用于检查文件和目录更改的类。
回答by Jigar Joshi
回答by Fluffy Panda
Why not Apache Camel?
为什么不是阿帕奇骆驼?
Here's what the code will look like:
下面是代码的样子:
from("file://pollingfolder?delete=true").to("bean:handleOrder");
That reads the files from "pollingfolder", deletes it upon read, and sends it to a bean called "handleOrder". Just in one line!
它从“pollingfolder”读取文件,读取时将其删除,然后将其发送到名为“handleOrder”的 bean。就在一行!
There's an easy way to configure it with Spring-boot automagically if you use Spring, but it can be used in plain Java as well.
如果您使用 Spring,有一种简单的方法可以使用 Spring-boot 自动配置它,但它也可以在普通 Java 中使用。
Source: Apache Camel
来源:Apache Camel