Panther
Panther

Reputation: 15

Continuously scan if a file exists in a folder

I have a folder with alot of different data types (xls, doc, ....). If there is a new file with some specific file extension (.avi) && the first letter of the file name starts with "EN_", the file should be moved to another folder. This should be run continuously, means when some file got moved it starts immediately with scanning for the next file with the specific properties (.avi & EN_). With my current knowledge I just would use a endless while loop. Is there maybe any other solution? How would you solve it?

Thanks in advance.

Upvotes: 1

Views: 767

Answers (3)

David S.
David S.

Reputation: 6705

With Java 8 you can create a WatchService.

It will allow you to watch for changes to a directory of a certain type.

Upvotes: 0

Konstantin Milyutin
Konstantin Milyutin

Reputation: 12366

You could try to use JNotify library, it should be more efficient than a while loop: http://jnotify.sourceforge.net/

I never used it, but I think this library makes use of OS specific features. For example, in Linux you have inotify, which allows you to listen for filesystem events.

P.S. As mentioned in another thread, you should you Java 7 new file API for this: How does Jnotify works

Upvotes: 1

Tenzin
Tenzin

Reputation: 2505

You could write a infinite loop that makes use of a try catch. Try to find the file, if it is not there it will execute the catch.

Upvotes: 0

Related Questions