This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes Watch a Folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Watch a Folder" Watch "Watch a Folder" New topic

Watch a Folder

Costa Lamona

Joined: Sep 17, 2006
Posts: 29

Is there some core java class that watches a folder so that when a file inserted into that folder something fires some event?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45286
Don't think so, no.

That is not the way applications usually work; you work from the application towards the OS/file system.

Try a Timer and a TimerTask which interrogates the folder regularly and records whether there is any change to the number of files in the folder. Go through the class for more details.
siva krishna pallapu

Joined: Aug 12, 2008
Posts: 7
i think we can do this with a thread which is running continiously iterating through a while loop and checking the folders last modifies time at regular intervals the folder.lastmodified() if there is change in the time we can fire an event . i hope so it works..
Jelle Klap

Joined: Mar 10, 2008
Posts: 1948

You could use the Apache Commons Virtual File System (VFS) classes to accomplish this if you don't feel like rolling your own.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
I agree. Here's the link:
subject: Watch a Folder
jQuery in Action, 3rd edition