code: public static void writeDocumentToFile(Document document,
                                             String filePathname, String method, int indent)
              throws TransformerException, IOException {
  
          Transformer transformer = TransformerFactory.newInstance()
                  .newTransformer();
          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
          transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
          transformer.setOutputProperty(OutputKeys.METHOD, method);
  
          transformer.transform(new DOMSource(document), new StreamResult(
                  new FileOutputStream(filePathname)));
      }
docs: Write the document object to a file.
  
  @param document     the document object.
  @param filePathname the path name of the file to be written to.
  @param method       the output method: for instance html, xml, text
  @param indent       amount of indentation. -1 to use the default.
  @throws TransformerException if an exception occurs.
  @throws IOException          if an IO exception occurs.