public static void renameFile ( File srcFile , File dstFile ) throws IOException { // Rename the srcFile file to the new one. Unfortunately, the renameTo() // method does not work reliably under some JVMs. Therefore, if the // rename fails, we manually rename by copying the srcFile file to the new one if ( ! srcFile . renameTo ( dstFile ) ) { InputStream in = null ; OutputStream out = null ; try { in = new FileInputStream ( srcFile ) ; out = new FileOutputStream ( dstFile ) ; transfer ( in , out ) ; } catch ( IOException ioe ) { IOException newExc = new IOException ( "Cannot rename " + srcFile + " to " + dstFile ) ; newExc . initCause ( ioe ) ; throw newExc ; } finally { if ( in != null ) { in . close ( ) ; } if ( out != null ) { out . close ( ) ; } } // delete the srcFile file. srcFile . delete ( ) ; } }
Rename file . If file can t be renamed in standard way the coping data will be used instead .