Directory listener in Java -
i have application in want listen changes made particular directory. application should ping me there files added, deleted or updated in directory.
you can use jnotify
jnotify java library allow java application listen file system events, such as: file created file modified file renamed file deleted supported platforms
windows (2000 or newer) windows notes linux inofity support (2.6.14 or newer) linux notes mac os x (10.5 or newer) mac os notes
more info :
download jnotify here
extract zip, put .dll/.so according platform in lib path. , create class provide jnotify-0.93.jar
in class path.
sample code:
package org.life.java.stackoverflow.questions; import net.contentobjects.jnotify.jnotify; import net.contentobjects.jnotify.jnotifylistener; /** * * @author jigar */ public class jnotifydemo { public void sample() throws exception { // path watch string path = system.getproperty("user.home"); // watch mask, specify events care about, // or jnotify.file_any events. int mask = jnotify.file_created | jnotify.file_deleted | jnotify.file_modified | jnotify.file_renamed; // watch subtree? boolean watchsubtree = true; // add actual watch int watchid = jnotify.addwatch(path, mask, watchsubtree, new listener()); // sleep little, application exit if // don't (watching asynchronous), depending on // application, may not required thread.sleep(1000000); // remove watch watch boolean res = jnotify.removewatch(watchid); if (!res) { // invalid watch id specified. } } class listener implements jnotifylistener { public void filerenamed(int wd, string rootpath, string oldname, string newname) { print("renamed " + rootpath + " : " + oldname + " -> " + newname); } public void filemodified(int wd, string rootpath, string name) { print("modified " + rootpath + " : " + name); } public void filedeleted(int wd, string rootpath, string name) { print("deleted " + rootpath + " : " + name); } public void filecreated(int wd, string rootpath, string name) { print("created " + rootpath + " : " + name); } void print(string msg) { system.err.println(msg); } } public static void main(string[] args) throws exception { new jnotifydemo().sample(); } }
output:
modified c:\documents , settings\jigar: locals~1\temp\etilqs_4s8ywsvyukghk0udxrop modified c:\documents , settings\jigar : locals~1\temp\etilqs_4s8ywsvyukghk0udxrop modified c:\documents , settings\jigar : locals~1\temp\output1295531079119 modified c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default deleted c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache\f_001ea9 created c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache\f_001eae modified c:\documents , settings\jigar : locals~1\temp\etilqs_04gchl79zjrpclziqiom modified c:\documents , settings\jigar : locals~1\temp\etilqs_04gchl79zjrpclziqiom modified c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache modified c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache\f_001eae modified c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache\f_001eae modified c:\documents , settings\jigar : locals~1\temp\output1295531079119 modified c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\current session deleted c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache\f_001ea8 created c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache\f_001eaf modified c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache modified c:\documents , settings\jigar : locals~1\temp\etilqs_04gchl79zjrpclziqiom modified c:\documents , settings\jigar : locals~1\temp\etilqs_04gchl79zjrpclziqiom modified c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache\f_001eaf modified c:\documents , settings\jigar : local settings\application data\google\chrome\user data\default\cache\f_001eaf
Comments
Post a Comment