code: public static void replaceFile(File src, File target) throws IOException {
      /* renameTo() has two limitations on Windows platform.
       * src.renameTo(target) fails if
       * 1) If target already exists OR
       * 2) If target is already open for reading/writing.
       */
      if (!src.renameTo(target)) {
        int retries = 5;
        while (target.exists() && !target.delete() && retries-- >= 0) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            throw new IOException("replaceFile interrupted.");
          }
        }
        if (!src.renameTo(target)) {
          throw new IOException("Unable to rename " + src +
                                " to " + target);
        }
      }
    }
docs: Move the src file to the name specified by target.
  @param src the source file
  @param target the target file
  @exception IOException If this operation fails