views:

130

answers:

4

hi all.
is there anyone know how to monitor a folder using java? or anyone could gave me a point that how could i start this. here's my thought about it.

start a thread to scan the folder changes,which could be create,delete,update files in this folder or something else happen,like last updated.
but in this case, you have to control thread loop. if this thread loop is not controlled well,then it would be a waste of cpu and may cause a fatal problem.

or,is there any framework or some demo code to do this ? hope we could find a better way to do this. thank u very much.

A: 

No it is not. There are some libraries doing the polling for you such as:

http://mindprod.com/jgloss/filemonitor.html that is free and http://www.teamdev.com/jxfilewatcher that is not free

"Ideally, you would like to avoid polling and have instant notification of changes. To do this would require OS-specific JNI code to hook into the native file system. This would also require high privilege."

Manuel Selva
via JNI that means to write cross platform code for java,it feels not right to me.
Barry Wei
jxfilewatcher is not free. i think maybe we could find an open source software or library to do this. if there's not,i will write it.
Barry Wei
The first link I sent is a free one I think
Manuel Selva
A: 

Your thought is not bad.

To monitor a folder in java, the usual way is to control it in a separate thread using a loop. In order to save CPU, you can use a tempo (check every n seconds).

Then, to notify a change from this thread, you can use the "Observer" design pattern.

Benoit Courtine
yeah,i know this look should be taken good care of. check folder every n seconds will do it well,but how should i determine such a seconds,because this code would be run for different CPU and different OS. that's not easy if i want to save CPU and other stuff. but,thanks ur reply.
Barry Wei
+5  A: 

JDK7's java.nio.file package has a WatchService to support file change notification. You can use it to monitor a directory for changes. This uses native facilities where available, otherwise it uses a primitive mechanism such as polling.

For now, you can try jpathwatch, which is an implementation of the WatchService interface and uses native OS functions, instead of polling.

dogbane
i think i have to wait for a long time for this,cuz my current jre is jre6.
Barry Wei
i've checked over this WatchService, it's great but only pity that it would be only available after jdk7.
Barry Wei
A: 

The pure Java way would be to spun off a thread that polls the directory and tracks changes.

A more efficient way would be to write an OS specific library (You'll probably have to do it in C or C++) that can use OS specific tools to obtain a notification (via a callback into your Java code), and call it via JNI

Visage
good idea.but if i use OS specific library that means my app will not be able to run in different OS.
Barry Wei
True. You could code it in such a way that if you're running on an OS where you have a library the code uses it, otherwise it defaults to a purely Java solution. In fact, as mentioned in other answers, this is what happens in Java 7
Visage