How can I lock a file using java (if possible)

前端 未结 8 1371
无人及你
无人及你 2020-11-21 23:48

I have a Java process that opens a file using a FileReader. How can I prevent another (Java) process from opening this file, or at least notify that second process that the

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 00:25

    This may not be what you are looking for, but in the interest of coming at a problem from another angle....

    Are these two Java processes that might want to access the same file in the same application? Perhaps you can just filter all access to the file through a single, synchronized method (or, even better, using JSR-166)? That way, you can control access to the file, and perhaps even queue access requests.

提交回复
热议问题