code: public static File createUniqueFile(File directory, String filename) throws IOException {
  
          File uniqueFile = new File(directory, filename);
          if (uniqueFile.createNewFile()) {
              return (uniqueFile);
          }
  
          String extension = "";
          int dotIndex = filename.lastIndexOf('.');
          if (dotIndex >= 0) {
              extension = filename.substring(dotIndex);
              filename = filename.substring(0, dotIndex);
          }
  
          int fileNumber = 0;
          while (!uniqueFile.createNewFile()) {
              fileNumber++;
              String numberedFilename = String.format("%s-%d%s", filename, fileNumber, extension);
              uniqueFile = new File(directory, numberedFilename);
          }
  
          return (uniqueFile);
      }
docs: Creates a File that is unique in the specified directory. If the specified
  filename exists in the directory, "-#" will be appended to the filename until
  a unique filename can be created.
  
  @param directory the directory to create the file in
  @param filename the base filename with extension
  @return a File that is unique in the specified directory
  @throws IOException if any error occurs during file creation