开发者

How to poll directory to check whether new file is added?

开发者 https://www.devze.com 2023-04-03 05:36 出处:网络
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.

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?


  • Java 7 has a file watcher API
  • JNotify will do it as well.


If you are using Java 7, you can use the filesystem watch service (a new feature in Java 7).

See Oracle's tutorial that explains how to use it.

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.


jNotify would be useful,

See Also

  • directory-listener-in-java


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!

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.

Source: Apache Camel

0

精彩评论

暂无评论...
验证码 换一张
取 消