code: public static void dumpUnit(CompilationUnit unit) {
      String relativeOutputPath = unit.getMainTypeName().replace('.', '/') + ".ast";
      File outputFile = new File(
          unit.getEnv().options().fileUtil().getOutputDirectory(), relativeOutputPath);
      outputFile.getParentFile().mkdirs();
  
      try (FileOutputStream fout = new FileOutputStream(outputFile);
          OutputStreamWriter out = new OutputStreamWriter(fout, "UTF-8")) {
        out.write(dump(unit));
      } catch (IOException e) {
        ErrorUtil.fatalError(e, outputFile.getPath());
      }
    }
docs: Dumps a compilation unit to a file. The output file's path is the
  same as where translated files get written, but with an "ast" suffix.