You can try to lock the file, and if the file is not opened by someone else, you will not get an exception, otherwise you will get an exception, so by writing this function, you can check if the file is already opened by someone else:
public boolean isFileOpened(File file) boolean res = false; FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); // Get an exclusive lock on the whole file FileLock lock = channel.lock(); try { //The file is not already opened lock = channel.tryLock(); } catch (OverlappingFileLockException e) { // File is open by someone else res = true; } finally { lock.release(); } return res; }
Then you can use this method to check if the file is open this way:
if(isFileOpened(someFile)) {
source share