polling - How to fire an event when a file is not been modified using Java WatchService? -


i want fire event whenever file has stopped being modified. polling directory , if filename matches "input.csv" want fire event on file. time check whether file has been modified 10 seconds. getting null pointer exception when no event occurs. sample code:

public class mypoller { public static void main(string[] args) throws urisyntaxexception,         ioexception, interruptedexception {     path tmppath = paths.get("e:/mydirectory/dec");     watchservice watchservice = filesystems.getdefault().newwatchservice();     // watching e:/mydirectory/dec directory     // modify , delete operations     tmppath.register(watcput.csvservice, standardwatcheventkinds.entry_modify,             standardwatcheventkinds.entry_delete);     (;;) {         watchkey key = watchservice.poll(10000,timeunit.milliseconds);          list<watchevent<?>> events = key.pollevents();          // poll events queued key         (watchevent event : events) {              switch (event.context().tostring()) {             case "input.csv":                 system.out.println("modified: " + event.context());                 string filename = "" + event.context();                 system.out.println(" "+filename);                  break;                  default:                     break;             }         }         boolean valid = key.reset();          // if key invalid, exit.         if (!valid) {             break;         }     }  } } 

if no event generated, watchkey may null

watchkey key = watchservice.poll(10000,timeunit.milliseconds); 

try null check on key , proceed pollevents() if it's indeed not null.


Comments