Fire an event whenever a file is added to a folder

Hi all I want a java code or any idea so that i can fire and handle an event whenever a file is added to a particular(known path) folder. please let me know if anybody has an idea thanksNaveen
[234 byte] By [nav_ninia] at [2007-11-27 6:27:37]
# 1
Smells like JNI or polling.
BIJ001a at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 2
> Smells like JNI or polling.Polling only, AKAIK. Most OS's don't fire events when a directory changes. BeOS does, but who uses that? (queue 50 angry responses from the isComputerOn mob...)
georgemca at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 3
I pre-emptively answer "what is polling?" :-)You need to have a thread running that periodically checks the directory in question for changes
georgemca at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 4
> Most OS's don't fire events when a directory changesAFAIK both Windows and Linux do.
BIJ001a at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 5
> Most OS's don't fire events when a directory changes.And Windows? Does Windows fire directory change events?If not, then Explorer must be polling the directories.
tom_jansena at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 6
> > Most OS's don't fire events when a directory> changes> > AFAIK both Windows and Linux do.And I'm equally sure they don't. Anyone got a definitive answer?
georgemca at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 7
I am sure for both a given directory can be registered so that the event is fired. On Linux I remember having read about it in some manual, it went through an open file descriptor referring to a directory.
BIJ001a at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 8

Googling for these:

"directory changed file descriptor register manual"

yields as first hit:

http://www.squarebox.co.uk/cgi-squarebox/manServer/fcntl.2

File and directory change notification

F_NOTIFY

(Linux 2.4 onwards) Provide notification when the directory referred to by fd or any of the files that it contains is changed. The events to be notified are specified in arg, which is a bit mask specified by ORing together zero or more of the following bits:

...etc..

BIJ001a at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 9
Hi I have MAC OS X and want to do something like ;that FileSystemWatcher does in .net i am new to java please let me know the APIs or study areathanks
nav_ninia at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 10

> Googling for these:

> "directory changed file descriptor register manual"

> yields as first hit:

> http://www.squarebox.co.uk/cgi-squarebox/manServer/fcn

> tl.2

>

>

> File and directory change notification

>

> _NOTIFY

> (Linux 2.4 onwards) Provide notification when the

> directory referred to by fd or any of the files that

> it contains is changed. The events to be notified are

> specified in arg, which is a bit mask specified by

> ORing together zero or more of the following bits:

>

>

> ...etc..

Cheers for that. I'm off to poke our BeOS evangelist in the eye for boasting that his beloved OS fired events on filesystem changes where others did not (although technically, something still must be polling, way down in the kernel....)

georgemca at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 11

> Hi

>

> I have MAC OS X and want to do something like ;

> that FileSystemWatcher does in .net

>

> i am new to java please let me know the APIs or study

> area

>

> thanks

If you're only going to use this on Mac OS X, then you could just do it in Objective C and Cocoa. The documentation that comes with XCode is excellent.

If you still want to use Java, then you are going to have to learn JNI to make this work, and you'll have to probably do the underlying Mac OS work in C.

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html

kevjavaa at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 12
Read this http://forum.java.sun.com/thread.jspa?threadID=788280&messageID=4484706#4484706REGARDS
Jamwaa at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...
# 13
> (although technically, something still must be polling, way down in the kernel....)No, the kernel *knows* when a file or a directory is changed and it can run through the list of the objects to be watched and check. I won't call this polling.
BIJ001a at 2007-7-12 17:49:42 > top of Java-index,Java Essentials,Java Programming...