public static void copyFile(File sourceFile,File destFile,Properties tokens,boolean overwrite,boolean preserveLastModified) throws IOException {
  if (overwrite || (!destFile.exists()) || (destFile.lastModified() < sourceFile.lastModified())) {
    if (destFile.exists() && destFile.isFile()) {
      destFile.delete();
    }
    File parent=destFile.getParentFile();
    if ((parent != null) && (!parent.exists())) {
      parent.mkdirs();
    }
    if (sourceFile.isDirectory()) {
      destFile.mkdirs();
      return;
    }
    String line=null;
    if ((tokens != null) && (!tokens.isEmpty())) {
      BufferedReader in=new BufferedReader(new FileReader(sourceFile));
      BufferedWriter out=new BufferedWriter(new FileWriter(destFile));
      try {
        while ((line=in.readLine()) != null) {
          if (line.length() > 0) {
            Enumeration tokensName=tokens.propertyNames();
            while (tokensName.hasMoreElements()) {
              String name=(String)tokensName.nextElement();
              if (line.indexOf(name) >= 0) {
                line=line.replaceAll(name,tokens.getProperty(name));
              }
            }
            out.write(line);
            out.newLine();
          }
 else {
            out.newLine();
          }
        }
      }
  finally {
        out.close();
        in.close();
      }
    }
 else {
      FileInputStream in=null;
      FileOutputStream out=null;
      try {
        in=new FileInputStream(sourceFile);
        out=new FileOutputStream(destFile);
        byte[] buffer=new byte[BUFFER_SIZE];
        int count=0;
        while ((count=in.read(buffer,0,buffer.length)) != -1) {
          out.write(buffer,0,count);
        }
      }
  finally {
        out.close();
        in.close();
      }
    }
    if (preserveLastModified) {
      destFile.setLastModified(sourceFile.lastModified());
    }
  }
}

public static void copyFile ( File sourceFile , File destFile , Properties tokens , boolean overwrite , boolean preserveLastModified ) throws IOException { if ( overwrite || ( ! destFile . exists ( ) ) || ( destFile . lastModified ( ) < sourceFile . lastModified ( ) ) ) { if ( destFile . exists ( ) && destFile . isFile ( ) ) { destFile . delete ( ) ; } File parent = destFile . getParentFile ( ) ; if ( ( parent != null ) && ( ! parent . exists ( ) ) ) { parent . mkdirs ( ) ; } if ( sourceFile . isDirectory ( ) ) { destFile . mkdirs ( ) ; return ; } String line = null ; if ( ( tokens != null ) && ( ! tokens . isEmpty ( ) ) ) { BufferedReader in = new BufferedReader ( new FileReader ( sourceFile ) ) ; BufferedWriter out = new BufferedWriter ( new FileWriter ( destFile ) ) ; try { while ( ( line = in . readLine ( ) ) != null ) { if ( line . length ( ) > 0 ) { Enumeration tokensName = tokens . propertyNames ( ) ; while ( tokensName . hasMoreElements ( ) ) { String name = ( String ) tokensName . nextElement ( ) ; if ( line . indexOf ( name ) >= 0 ) { line = line . replaceAll ( name , tokens . getProperty ( name ) ) ; } } out . write ( line ) ; out . newLine ( ) ; } else { out . newLine ( ) ; } } } finally { out . close ( ) ; in . close ( ) ; } } else { FileInputStream in = null ; FileOutputStream out = null ; try { in = new FileInputStream ( sourceFile ) ; out = new FileOutputStream ( destFile ) ; byte [ ] buffer = new byte [ BUFFER_SIZE ] ; int count = 0 ; while ( ( count = in . read ( buffer , 0 , buffer . length ) ) != - 1 ) { out . write ( buffer , 0 , count ) ; } } finally { out . close ( ) ; in . close ( ) ; } } if ( preserveLastModified ) { destFile . setLastModified ( sourceFile . lastModified ( ) ) ; } } }
copies file from source to destination . the destination directories will be created if it is not in the system
