How to prevent file overriding while reading and processing it using Java?

I will need to read and process several large files using Java, and I would like to know if there is some reasonable way to protect the file, which it will not overwrite with other processes while I read and process it?

That is, somehow make it read-only, keep it "open" or something like that.

This will be done in a Windows environment.

br, Toko

+4
source share
2 answers

you want filelock :

FileChannel channel = new RandomAccessFile("C:\\foo", "rw").getChannel(); // Try acquiring the lock without blocking. This method returns // null or throws an exception if the file is already locked. FileLock lock = channel.tryLock(); // ... // release it lock.release(); 

for simplicity, I omitted the tryng finally finally block, but you should not in your production code

+10
source

The work is mostly excellent, some problems have arisen: since the lock does not seem absolutely absolute .

0
source

All Articles