public static boolean copyFile ( final File source , final File destination , final Charset sourceEncoding , final Charset destinationEncoding , final boolean lastModified ) throws IOException { if ( source . isDirectory ( ) ) { throw new IllegalArgumentException ( "The source File " + destination . getName ( ) + " should be a File but is a Directory." ) ; } if ( destination . isDirectory ( ) ) { throw new IllegalArgumentException ( "The destination File " + destination . getName ( ) + " should be a File but is a Directory." ) ; } boolean copied = false ; try ( InputStream inputStream = StreamExtensions . getInputStream ( source ) ; InputStreamReader reader = sourceEncoding != null ? new InputStreamReader ( inputStream , sourceEncoding ) : new InputStreamReader ( inputStream ) ; OutputStream outputStream = StreamExtensions . getOutputStream ( destination , ! destination . exists ( ) ) ; BufferedOutputStream bos = new BufferedOutputStream ( outputStream ) ; OutputStreamWriter writer = destinationEncoding != null ? new OutputStreamWriter ( bos , destinationEncoding ) : new OutputStreamWriter ( bos ) ) { int tmp ; final char [ ] charArray = new char [ FileConst . BLOCKSIZE ] ; while ( ( tmp = reader . read ( charArray ) ) > 0 ) { writer . write ( charArray , 0 , tmp ) ; } copied = true ; } catch ( final IOException e ) { throw e ; } if ( lastModified ) { destination . setLastModified ( source . lastModified ( ) ) ; } return copied ; }
Copies the given source file to the given destination file with the given source encodings and destination encodings .