private void writeJsonInFile(String jsonOutput, String filename) throws IOException {
    File jsonOutputFile;
    try {
      jsonOutputFile = new File(filename);
      if (jsonOutputFile.exists()) {
        File oldFile = new File(jsonOutputFile.getAbsolutePath() + "_old");
        System.out.println("Output file already exists, renaming to " + oldFile);
        jsonOutputFile.renameTo(oldFile);
      }
      if (jsonOutputFile.createNewFile()) {
        System.out.println("File created: " + jsonOutputFile.getName());
      } else {
        System.out.println("File already exists.");
      }
    } catch (IOException e) {
      System.out.println("IOException occurred: " + e);
      throw e;
    }

    // Try block to check for exceptions
    try (PrintWriter pw = new PrintWriter(jsonOutputFile)) {
      pw.println(jsonOutput);
      pw.flush();
      System.out.println("Summary written to " + jsonOutputFile);
    } catch (IOException ex) {
      // Print message as exception occurred when invalid path of local machine is passed
      System.out.println("Failed to write output file:" + ex.getMessage());
      throw ex;
    }
  }
Helper method of exportInJson.

@param jsonOutput A string, JSON formatted string about metadataSummary.
@param filename   Path of a file in String where the summary needs to be output to.
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/metatool/MetaToolTaskMetadataSummary.java