This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Monitoring Folders Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "Monitoring Folders" Watch "Monitoring Folders" New topic

Monitoring Folders

hatim osman
Ranch Hand

Joined: Jun 10, 2005
Posts: 105
Hi there
Is there way I can monitor a particular folder, new files added, renamed or deleted? I want so some sort of a listener, if possible of course.

thank you
Adam Teg
Ranch Hand

Joined: Jul 10, 2007
Posts: 58
Currently doing a simillar thing. I implemented a Thread(s) that wakes up every 10 seconds to check if a file came in or not. Looks like in your case you will need to know what files were there to begin with etc.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12792
I know of no way to get the operating system to do a call-back to your Java program when the file system is modified so it looks like you will end up using the methods in

A File object can designate a directory or a disk file. It has the method lastModified() - I wonder what happens when you call lastModified on a File object that represents a directory??

Vinod Awar
Ranch Hand

Joined: Nov 06, 2006
Posts: 125
Write an interface Monitor with methods like addMonitor and removeMonitor
and you can monitor the folder for any type of files by listing all files in the folder and checking their extension. If found you can invoke a filefound() method or a listener registered with this. Just a vague idea

The biggest bankruptcy is the loss of enthusiasm
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

Originally posted by hatim osman:
Hi there
Is there way I can monitor a particular folder, new files added, renamed or deleted? I want so some sort of a listener, if possible of course.

thank you

Other actions can be deduced but i am really not sure about how a rename action can be deduced i.e. how do you differentiate between a file deleted and a new created and one is renamed to other? I dont think there is a way to find out whether the file/directory has been renamed unless OS gives you a callback for this(which as william pointed out is not available.)
Since, you can not have a listener for directory modification events, so you have to write a deamon that wakes up after a configured interval of time and checks the state of the dir with the last state.
There can be an optimization(as william pointed) that you can check the last modified time of the directory with the last time it was scanned. But, this depends on the OS whether it updates the time modified of the directory for actions taken on any of the files and sub-directories directly by the OS or through other application. (I validated it with windows vista, the directory time does not change if i modify any of its file using my IDE.)

apigee, a better way to API!
It is sorta covered in the JavaRanch Style Guide.
subject: Monitoring Folders