public static void copyDirectory ( File source , File target ) { if ( source . isDirectory ( ) ) { if ( ! target . exists ( ) ) { target . mkdir ( ) ; } for ( String child : source . list ( ) ) { copyDirectory ( new File ( source , child ) , new File ( target , child ) ) ; } } else { try { write ( new FileInputStream ( source ) , new FileOutputStream ( target ) ) ; } catch ( IOException e ) { if ( target . isDirectory ( ) ) { if ( ! target . exists ( ) ) { if ( ! target . mkdirs ( ) ) { throw E . ioException ( "cannot copy [%s] to [%s]" , source , target ) ; } } target = new File ( target , source . getName ( ) ) ; } else { File targetFolder = target . getParentFile ( ) ; if ( ! targetFolder . exists ( ) ) { if ( ! targetFolder . mkdirs ( ) ) { throw E . ioException ( "cannot copy [%s] to [%s]" , source , target ) ; } } } try { write ( new FileInputStream ( source ) , new FileOutputStream ( target ) ) ; } catch ( IOException e0 ) { throw E . ioException ( e0 ) ; } } } }
If target does not exist it will be created .