public static void copyFile ( File src , File dest ) throws IOException { if ( src == null ) throw new NullPointerException ( "Source must not be null" ) ; if ( dest == null ) throw new NullPointerException ( "Destination must not be null" ) ; if ( ! src . exists ( ) ) throw new FileNotFoundException ( "Source '" + src + "' does not exist" ) ; if ( ! src . isFile ( ) ) throw new IOException ( "Source '" + src + "' is not a file" ) ; if ( dest . exists ( ) ) throw new IOException ( "Destination '" + dest + "' is already exists" ) ; FileChannel in = null , out = null ; try { in = new FileInputStream ( src ) . getChannel ( ) ; out = new FileOutputStream ( dest ) . getChannel ( ) ; in . transferTo ( 0 , in . size ( ) , out ) ; } finally { try { in . close ( ) ; out . close ( ) ; } catch ( IOException e ) { // Ignore. } } if ( src . length ( ) != dest . length ( ) ) { throw new IOException ( "Failed to copy full contents from '" + src + "' to '" + dest + "'" ) ; } dest . setLastModified ( src . lastModified ( ) ) ; }
Copies a file to a new location preserving the file date .