nio - Java - how to find out if a directory is being used by another process? -
i want use java 7's watchservice monitor changes directory.
seems tries lock folder, , throw exception if fails, not seem provide method of locking before-hand / checking if locked.
i need know if directory being used process or not. since can't lock or open stream (because it's directory), i'm looking more intelligent trying modify , sleeping if failed, or try/catch sleep.
ideally, blocking call until available.
edit: can't seem acquire filelock on folder. when try lock folder, "filenotfoundexception (access denied)". googling suggests can't use object on directory.
registration code:
watchservice watchservice = path.getfilesystem().newwatchservice() path.register(watchservice, standardwatcheventkinds.entry_create, standardwatcheventkinds.entry_modify, standardwatcheventkinds.entry_delete)
failing scenario:
let's i'm listening folder f
new creation.
if sub-folder g
created in it, want listen changes in g
. however, if create new folder in f
(in windows), fail because windows locking folder until name given.
thanks
taken here
file file = new file(filename); filechannel channel = new randomaccessfile(file, "rw").getchannel(); // exclusive lock on whole file filelock lock = channel.lock(); try { lock = channel.trylock(); // ok. lock } catch (overlappingfilelockexception e) { // file open else } { lock.release(); }
Comments
Post a Comment