File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Monitoring a Directory Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Monitoring a Directory" Watch "Monitoring a Directory" New topic
Author

Monitoring a Directory

Christopher Mundt
Greenhorn

Joined: Jan 19, 2006
Posts: 1
Can anyone tell me the best way to monitor a directory for file changes? I have code that currently uses a properties file to store file modification times. This works but as the number of files increase with run time so does the memory requirement.

I need to monitor 1 or more directories in live time and process new files.

Thanks,
Chris
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
As far as I know, Java does not provide a way to monitor directories or files for changes.

Some operating systems do, so one approach might be to use some native code (JNI) to hook into the operating system facility.

If that sounds too scary and/or time-consuming, then you must implement a pure-Java directory scanner. I've done this in the past. A background "scanner" thread reads the directory in question (File.list(), I think) every few seconds, and compares the current contents to the previous contents (which your Java code would have to store). Other objects can register with the scanner object, to receive notifications of the arrival of new files.

A pitfall is that it is not wise to notify clients immediately when a new file is seen. The reason is that the file may still be being written. My scanner remembers the previous size and modification time of each file and only notifies clients after these have remained stable for several scans.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
I did this by creating a thread and watching the dir for events. When I see them, I use a Runtime instance to invoke a dif program (do a search on dif programs and you will find a bunch of them that can compare directory changes). The dif program then creates a report and puts that report in another directory. My report happens to be HTML so I have to parse it using an HTML parser (this was a constraint of my particular project).

There may be a much easier way but all-in-all, this was very easy to do and is providing excellent results for me.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
One other thing you might consider is the java.io.File class. It has methods to let you read the contents of directories and get the properties of files.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Monitoring a Directory
 
Similar Threads
monitor /proc/net/dev
How to create a .war file?
The ULTIMATE! From the creator of FileMonitor comes DirectoryMonitor :)
Starting Glassfish as an application server with MyEclipse
Monitoring Folders