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
source share