code: public static void saveKeyStore(File targetStoreFile, KeyStore store, String password) {
          File folder = targetStoreFile.getAbsoluteFile().getParentFile();
          if (!folder.exists()) {
              folder.mkdirs();
          }
          File tmpFile = new File(folder, Long.toHexString(System.currentTimeMillis()) + ".tmp");
          FileOutputStream fos = null;
          try {
              fos = new FileOutputStream(tmpFile);
              store.store(fos, password.toCharArray());
              fos.flush();
              fos.close();
              if (targetStoreFile.exists()) {
                  targetStoreFile.delete();
              }
              tmpFile.renameTo(targetStoreFile);
          } catch (IOException e) {
              String message = e.getMessage().toLowerCase();
              if (message.contains("illegal key size")) {
                  throw new RuntimeException("Illegal Key Size! You might consider installing the JCE Unlimited Strength Jurisdiction Policy files for your JVM.");
              } else {
                  throw new RuntimeException("Could not save keystore " + targetStoreFile, e);
              }
          } catch (Exception e) {
              throw new RuntimeException("Could not save keystore " + targetStoreFile, e);
          } finally {
              if (fos != null) {
                  try {
                      fos.close();
                  } catch (IOException e) {
                  }
              }
  
              if (tmpFile.exists()) {
                  tmpFile.delete();
              }
          }
      }
docs: Saves the keystore to the specified file.
  
  @param targetStoreFile
  @param store
  @param password