java - WatchService Api file watching throws exception when try access file in created event occur -


i have code below tracking file changes on dedicate folder

 path path = paths.get("f:\\logs");     watchservice watchservice = filesystems.getdefault().newwatchservice();     watchkey key = path.register(watchservice, standardwatcheventkinds.entry_create);      while (true) {         final watchkey watchkey = watchservice.take();         (watchevent<?> watchevent : key.pollevents()) {             watchevent.kind<?> kind = watchevent.kind();             if (kind == standardwatcheventkinds.entry_create) {                 watchevent<path> eventpath = (watchevent<path>) watchevent;                 path newfilepath = eventpath.context();                 boolean writable = false;                 system.out.println(writable);                 long size = files.size(newfilepath) / (1000 * 1000);                  system.out.println(newfilepath.toabsolutepath() + "wriable:" + writable + "size:" + size);                 watchkey.reset();              }         }     } 

but when have file (named=newfile.dat) created , program hit line:

long size = files.size(newfilepath) / (1000 * 1000);

it throws

java.nio.file.nosuchfileexception: newfile.dat

and variable

writable 

always= false (even if try put while loop , sleep recheck)

please tell happened ??

i find out variable newfilepath relative path :( :(

i work around use

path dir = (path) watchkey.watchable(); path fullpath = dir.resolve(newfilepath); 

fullpath correctly path of file

but wonder, 's crap code :( :(


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -