コード例 #1
0
  /**
   * Simply write a String into a text file.
   *
   * @param string The String to write
   * @param file The file to write to (will be created if it doesn't exist
   * @param encoding The character encoding to use. If null, a standard utf-8 encoding will be used
   * @param append Whether to append the text to an existing file (true), or to overwrite it (false)
   * @return
   * @throws IOException
   */
  public static File writeTextFile(String string, File file, String encoding, boolean append)
      throws IOException {

    if (encoding == null) encoding = CommonUtils.encoding;

    FileWriterWithEncoding writer = null;
    writer = new FileWriterWithEncoding(file, encoding, append);
    writer.write(string);
    if (writer != null) writer.close();

    return file;
  }