java - WatchService WatchEvent .context() method returns inconsistent relative path for file on ENTRY_MODIFY (goutputstream-####, Linux OS) -
java - WatchService WatchEvent .context() method returns inconsistent relative path for file on ENTRY_MODIFY (goutputstream-####, Linux OS) -
in code, i'm hoping update hashmap recent version of given path's contents, absolute path string beingness used key. problem watchevent's .context() method giving me different relative path same file on each event.
here snippet of code:
else if(event.kind()==standardwatcheventkinds.entry_modify) { /*variable path path of "//workspaces", set earlier.*/ path oldfilepath=path.resolve((path)event.context()); /*problem line*/ string oldfilepathstring = oldfilepath.tostring(); filereader oldin = new filereader(oldfilepathstring); bufferedreader br = new bufferedreader(oldin); string line; list<string> newfiletext=new linkedlist<>(); while((line = br.readline())!=null) newfiletext.add(line); list<string> previoustext=new linkedlist<>(); if((previoustext = filemappings.get(oldfilepathstring))!= null) { system.out.println("previoustext:\n"+previoustext); system.out.println("newfiletext:\n"+newfiletext); } filemappings.put(oldfilepathstring, newfiletext); system.out.println(filemappings.keyset()+"\n"+filemappings.values()); } } and here sample output upon modifying file b.txt in watched directory contents "abc" "abc 123"
note of comes opening file /workspaces/b.txt (which exists) , modifying contents.):
run: entry_create:.goutputstream-brc1hx entry_modify:.goutputstream-brc1hx [/workspaces/.goutputstream-brc1hx] [[]] entry_modify:.goutputstream-brc1hx previoustext: [] newfiletext: [abc] [/workspaces/.goutputstream-brc1hx] [[abc]] entry_create:b.txt~ entry_create:b.txt entry_create:.goutputstream-mfj6hx entry_modify:.goutputstream-mfj6hx [/workspaces/.goutputstream-mfj6hx, /workspaces/.goutputstream-brc1hx] [[], [abc]] entry_modify:.goutputstream-mfj6hx previoustext: [] newfiletext: [abc, 123] [/workspaces/.goutputstream-mfj6hx, /workspaces/.goutputstream-brc1hx] [[abc, 123], [abc]] entry_create:b.txt~ entry_create:b.txtthe line of involvement path oldfilepath=path.resolve((path)event.context());
note how oldfilepath has resolved "/workspaces/.goutputstream-mfj6hx", , later "/workspaces/.goutputstream-brc1hx" same file.
event.context() returning different path same file after each modification. linux issue, or java issue, , how standard relative path (in case, it'd "b.txt") file?
it seems when perform modify, i'm getting sequence of create/modify/create events, , entry_creates have right filename, wile entry_modifys have temp handle (i'm guessing temp version of file used between saves.) need able capture file modification , pull right filename out of event.
i understand filesystem may doing temp file creation & processing under hood while i'm opening, modifying , saving file, how extract proper filename out of temp file event indicating entry_modify gives me? there sort of method grouping events pertaining modify, can find enclosing entry_create , filename that? or somehow traverse upward through stack of calls leading entry_create?
i can see filename in enclosing entry_create events, surrounding each entry_modify, i'm hoping there's more elegant way somehow (get recent event wasn't entry_modify, , .context() that.)
thanks!
i faced same question. think has nil linux problem or java issues. way editor of b.txt (i assume gedit) handles thing.
upon save it
creates new temp file ".goutputstream-xxxx" random xxxx (the create see),
writes new content file (the modify see),
renames original file b.txt~ (the create see),
and renames temp file b.txt (the create see)
so guess have watch out both entry_modify , entry_create respect b.txt see file modifications.
java watchservice
Comments
Post a Comment