Did you do some Google search on this topic: Here is some I came up with
- Java: Check if file is already open
- check if a file is already open before trying to delete it
- checking that an excel file is already opened by another application
I am copying the answer from the first question on stackoverflow here:
File file = new File(fileName);
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
// Get an exclusive lock on the whole file
FileLock lock = channel.lock();
try {
lock = channel.tryLock();
// Ok. You get the lock
} catch (OverlappingFileLockException e) {
// File is open by someone else
} finally {
lock.release();
}
Hope this will be of help.