Avatar of JodiR
JodiR
 asked on

How do I watch a directory for a file to appear in java?

I want to have a Java application running that will continuously watch several directories for files to appear, once they appear the applicatiion will do some processing on them. I figure I can code this but thought I'd ask so I don't run into any problems with the computer freezing up, high overhead etc. Any proven code out there?
Java

Avatar of undefined
Last Comment
AIndy

8/22/2022 - Mon
CEHJ

Use Timer and TimerTask.
wide_awake

I'd suggest having the Java program check for new files at specific intervals (depending on how frequently these files will be generated), then process any new files, then go back to sleep until the next interval.

You'll also want to maintain a list (Vector?) of filenames you've already processed, so you don't keep processing the same ones over & over.
CEHJ

>>then go back to sleep until the next interval.

no need to implement that yourself, i.e. reinvent the wheel. That's what TimerTask is for.
Your help has saved me hundreds of hours of internet surfing.
fblack61
wide_awake

>>no need to reinvent the wheel.

agreed 100%.  That's one of the best things about Java.  the above comment was just a rough description of what should happen.  The implementation was left as an exercise to the reader ;)
ASKER CERTIFIED SOLUTION
AIndy

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.