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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With