There is no pure Java solution for this. In fact, I do not think that there is a solution that does not have potentially unpleasant consequences. The main problem is that UNIX / LINUX does not have the ability to temporarily set the required file lock. (The Linux script to lock the file is flock , but the start style locks are discretionary. An application that does not bother the flock file will not be affected by other application locks in the file.)
The best you can do is use chattr +i to set the immutable attribute to the file. Unfortunately, this has other effects:
An immutable file cannot be written to or linked to.
If your application crashes without disabling this attribute, the user has a file that he / she mysteriously cannot modify or delete. Not even with sudo or su.
source share