public static boolean renameFile(final File fromFile,final File toFile){
  File toFile2=toFile;
  if (fromFile.renameTo(toFile)) {
    return true;
  }
  if (!fromFile.exists()) {
    return false;
  }
  if (!fromFile.canRead()) {
    return false;
  }
  if (toFile.isDirectory()) {
    toFile2=new File(toFile,fromFile.getName());
  }
  if (toFile2.exists()) {
    return false;
  }
  String parent=toFile2.getParent();
  if (parent == null)   parent=System.getProperty("user.dir");
  File dir=new File(parent);
  if (!dir.exists()) {
    return false;
  }
  if (!dir.canWrite()) {
    return false;
  }
  try {
    copyFile(fromFile,toFile2);
  }
 catch (  UncheckedIOException e) {
    if (toFile2.exists())     toFile2.delete();
    return false;
  }
  fromFile.delete();
  return true;
}

public static boolean renameFile ( final File fromFile , final File toFile ) { File toFile2 = toFile ; if ( fromFile . renameTo ( toFile ) ) { return true ; } if ( ! fromFile . exists ( ) ) { return false ; } if ( ! fromFile . canRead ( ) ) { return false ; } if ( toFile . isDirectory ( ) ) { toFile2 = new File ( toFile , fromFile . getName ( ) ) ; } if ( toFile2 . exists ( ) ) { return false ; } String parent = toFile2 . getParent ( ) ; if ( parent == null ) parent = System . getProperty ( "user.dir" ) ; File dir = new File ( parent ) ; if ( ! dir . exists ( ) ) { return false ; } if ( ! dir . canWrite ( ) ) { return false ; } try { copyFile ( fromFile , toFile2 ) ; } catch ( UncheckedIOException e ) { if ( toFile2 . exists ( ) ) toFile2 . delete ( ) ; return false ; } fromFile . delete ( ) ; return true ; }
attempts to rename a file . the built - in method file . renameto ( ) often fails for different reasons ( e . g . if the file should be moved across different file systems ) . this method first tries the method file . renameto ( ) , but tries other possibilities if the first one fails .
