code: public static boolean copy(File sourceFile, File targetFile, boolean overwrite) throws FileAlreadyExistsException, IOException {
          boolean overwriteOccurred = false;
  
          // check if the targetFile already exists
          if (targetFile.exists()) {
              // if overwrite is not allowed, throw an exception
              if (!overwrite) {
                  throw new FileAlreadyExistsException("Target file " + targetFile + " already exists");
              } else {
                  // set the flag that it occurred
                  overwriteOccurred = true;
              }
          }
  
          // proceed with copy
          FileInputStream fis = new FileInputStream(sourceFile);
          FileOutputStream fos = new FileOutputStream(targetFile);
          fis.getChannel().transferTo(0, sourceFile.length(), fos.getChannel());
          fis.close();
          fos.flush();
          fos.close();
  
          return overwriteOccurred;
      }
docs: Copy the source file to the target file while optionally permitting an
  overwrite to occur in case the target file already exists.
  @param sourceFile The source file to copy from
  @param targetFile The target file to copy to
  @return True if an overwrite occurred, otherwise false.
  @throws FileAlreadyExistsException Thrown if the target file already
  exists and an overwrite is not permitted.  This exception is a
  subclass of IOException, so catching an IOException is enough if you
  don't care about this specific reason.
  @throws IOException Thrown if an error during the copy