Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

directory

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?

like image 681
ramesh Avatar asked Sep 08 '11 07:09

ramesh


People also ask

Can we monitor a directory for adding new files in Java?

Create a WatchService "watcher" for the file system. For each directory that you want monitored, register it with the watcher. When registering a directory, you specify the type of events for which you want notification. You receive a WatchKey instance for each directory that you register.

What is a file watcher?

File Watcher is an IntelliJ IDEA tool that allows you to automatically run a command-line tool like compilers, formatters, or linters when you change or save a file in the IDE.

Which method to check if a file name is a directory?

isDirectory() checks whether a file with the specified abstract path name is a directory or not. This method returns true if the file specified by the abstract path name is a directory and false otherwise.

What is poll folder?

The Folder Poll tool for Windows helps to automate your data analysis process. Automatically perform operations when new files are detected in a monitored folder.


2 Answers

  • Java 7 has a file watcher API
  • JNotify will do it as well.
like image 174
TofuBeer Avatar answered Sep 23 '22 02:09

TofuBeer


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

like image 38
Fluffy Panda Avatar answered Sep 25 '22 02:09

Fluffy Panda