public static void copyFileToDirectory(File srcFile,File destDir,boolean preserveFileDate) throws IOException, IllegalArgumentException {
  if (srcFile == null) {
    throw new IllegalArgumentException("Source is null");
  }
  if (!srcFile.exists()) {
    throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
  }
  if (srcFile.isDirectory()) {
    throw new IOException("Source '" + srcFile + "' is a directory");
  }
  if (destDir == null) {
    throw new IllegalArgumentException("Destination is null");
  }
  if (!destDir.exists()) {
    if (!destDir.mkdir()) {
      throw new IOException("Destination '" + destDir + "' directory cannot be created");
    }
  }
 else {
    if (!destDir.isDirectory()) {
      throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
    }
  }
  File destFile=new File(destDir,srcFile.getName());
  if (destFile.exists() && !destFile.canWrite()) {
    throw new IOException("Destination '" + destFile + "' file exists but is read-only");
  }
  FileInputStream input=new FileInputStream(srcFile);
  FileOutputStream output=null;
  try {
    output=new FileOutputStream(destFile);
    byte[] buffer=new byte[1024];
    int length;
    while ((length=input.read(buffer)) > 0) {
      output.write(buffer,0,length);
    }
  }
  finally {
    CloseableUtils.tryToClose(input);
    CloseableUtils.tryToClose(output);
  }
  if (srcFile.length() != destFile.length()) {
    throw new IOException("Failed to copy from '" + srcFile + "' to '"+ destFile+ "'");
  }
  if (preserveFileDate)   destFile.setLastModified(srcFile.lastModified());
}

public static void copyFileToDirectory ( File srcFile , File destDir , boolean preserveFileDate ) throws IOException , IllegalArgumentException { if ( srcFile == null ) { throw new IllegalArgumentException ( "Source is null" ) ; } if ( ! srcFile . exists ( ) ) { throw new FileNotFoundException ( "Source '" + srcFile + "' does not exist" ) ; } if ( srcFile . isDirectory ( ) ) { throw new IOException ( "Source '" + srcFile + "' is a directory" ) ; } if ( destDir == null ) { throw new IllegalArgumentException ( "Destination is null" ) ; } if ( ! destDir . exists ( ) ) { if ( ! destDir . mkdir ( ) ) { throw new IOException ( "Destination '" + destDir + "' directory cannot be created" ) ; } } else { if ( ! destDir . isDirectory ( ) ) { throw new IllegalArgumentException ( "Destination '" + destDir + "' is not a directory" ) ; } } File destFile = new File ( destDir , srcFile . getName ( ) ) ; if ( destFile . exists ( ) && ! destFile . canWrite ( ) ) { throw new IOException ( "Destination '" + destFile + "' file exists but is read-only" ) ; } FileInputStream input = new FileInputStream ( srcFile ) ; FileOutputStream output = null ; try { output = new FileOutputStream ( destFile ) ; byte [ ] buffer = new byte [ 1024 ] ; int length ; while ( ( length = input . read ( buffer ) ) > 0 ) { output . write ( buffer , 0 , length ) ; } } finally { CloseableUtils . tryToClose ( input ) ; CloseableUtils . tryToClose ( output ) ; } if ( srcFile . length ( ) != destFile . length ( ) ) { throw new IOException ( "Failed to copy from '" + srcFile + "' to '" + destFile + "'" ) ; } if ( preserveFileDate ) destFile . setLastModified ( srcFile . lastModified ( ) ) ; }
copy a file to a directory
