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.
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.
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.