public static boolean delete(File file,boolean deleteOnExit){
  if (file == null)   throw new NullPointerException("File cannot be null");
  if (!file.exists())   return true;
  if (file.isDirectory()) {
    File[] files=file.listFiles();
    for (int i=0; i < files.length; i++)     delete(files[i],deleteOnExit);
  }
  if (!file.delete()) {
    if (Utilities.isWindows() && file.isFile()) {
      for (int i=0; i < 5; i++) {
        System.gc();
        if (file.delete())         return true;
      }
    }
    if (deleteOnExit)     file.deleteOnExit();
    return false;
  }
  return true;
}

public static boolean delete ( File file , boolean deleteOnExit ) { if ( file == null ) throw new NullPointerException ( "File cannot be null" ) ; if ( ! file . exists ( ) ) return true ; if ( file . isDirectory ( ) ) { File [ ] files = file . listFiles ( ) ; for ( int i = 0 ; i < files . length ; i ++ ) delete ( files [ i ] , deleteOnExit ) ; } if ( ! file . delete ( ) ) { if ( Utilities . isWindows ( ) && file . isFile ( ) ) { for ( int i = 0 ; i < 5 ; i ++ ) { System . gc ( ) ; if ( file . delete ( ) ) return true ; } } if ( deleteOnExit ) file . deleteOnExit ( ) ; return false ; } return true ; }
deletes file or folder . optionally invokes deleteonexit if necessary .
