Java FileLock for Reading and Writing

[亡魂溺海] 提交于 2019-11-28 07:27:31
user207421

(a) Are you aware that locking the file won't keep other processes from touching it unless they also use locks?
(b) You have to lock via a writable channel. Get the lock via a RandomAccessFile in "rw" mode and then open your FileInputStream. Make sure to close both!

It would be better if you created the lock using tryLock(0L, Long.MAX_VALUE, true).

This creates a shared lock which is the right thing to do for reading.

tryLock() is a shorthand for tryLock(0L, Long.MAX_VALUE, false), i.e. it requests an exclusive write-lock.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!