title: Check if file or folder is open before delete
body: <ul>
  <li>How to check if a file / folder is open before i try to delete it.</li>
  <li><p>Am deleting it programmatically.. before deleting, need to check whether its opened or not</p></li>
  <li><p>I want something like this,</p>
  
  <pre><code>            if(file/folder is open){
                 //do not delete it
              }else{
                 //delete it
               }
  </code></pre>
  
  <p>I tried the below set of two codes, but nothing is working</p>
  
  <pre><code>     File scrptFile=new File(dirFile);
       boolean isFileUnlocked = false;
  try {
      org.apache.commons.io.FileUtils.touch(scrptFile);
      isFileUnlocked = true;
  } catch (IOException e) {
      isFileUnlocked = false;
  }
  
  if(isFileUnlocked){
      // Do stuff you need to do with a file that is NOT locked.
      System.out.println("file is not locked");
  } else {
      // Do stuff you need to do with a file that IS locked
      System.out.println("file is locked");
  }
  
  
         File file = new File(dirFile);
  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
      System.out.println("Ok. You get the lock");
  } catch (OverlappingFileLockException e) {
      // File is open by someone else
      System.out.println("File is open by someone else");
  } finally {
      lock.release();
  }
  </code></pre></li>
  </ul>