Use JNotify. All you have to do is add jnotify.jar to the build path and put two dll files ie jnotify.dll jnotify_64bit.dll and inside lib jdk. Demo program
package jnotify; import net.contentobjects.jnotify.JNotify; import net.contentobjects.jnotify.JNotifyListener; public class MyJNotify { public void sample() throws Exception { String path = "Any Folder location here which you want to monitor"; System.out.println(path); int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; boolean watchSubtree = true; int watchID = JNotify .addWatch(path, mask, watchSubtree, new Listener()); Thread.sleep(1000000); boolean res = JNotify.removeWatch(watchID); if (!res) { System.out.println("Invalid"); } } 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) { try { new MyJNotify().sample(); } catch (Exception e) {
source share