code: public static void writeToFile(String str, String fileName, Charset charset) throws Exception {
      OutputStreamWriter writer = null;
      if (null == charset) {
        writer = new OutputStreamWriter(new FileOutputStream(fileName));
      } else {
        writer = new OutputStreamWriter(new FileOutputStream(fileName), charset.name());
      }
      writer.write(str);
      writer.close();
    }
docs: <p>
  writeToFile.
  </p>