Java File Append appendContent(File file, String content, String encoding)

Here you can find the source of appendContent(File file, String content, String encoding)

Description

Append text content to file, with encoding.

License

Open Source License

Parameter

Parameter Description
filepath File to be appended.
content String content to be appended to file.
encoding Encoding used for string content.

Exception

Parameter Description
IOException an exception

Declaration

public static void appendContent(File file, String content, String encoding) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;
import java.io.Writer;

public class Main {
    /**/*from w w  w. jav a2  s.  com*/
     * Append text content to file.
     * 
     * @param filepath
     *          Path to file
     * @param content
     *          String content to be appended to file.
     * @throws IOException
     */
    public static void appendContent(String filepath, String content) throws IOException {
        appendContent(new File(filepath), content);
    }

    /**
     * Append text content to file.
     * 
     * @param filepath
     *          File to be appended.
     * @param content
     *          String content to be appended to file.
     * @throws IOException
     */
    public static void appendContent(File file, String content) throws IOException {
        appendContent(file, content, "UTF-8");
    }

    /**
     * Append text content to file, with encoding.
     * 
     * @param filepath
     *          Path to file
     * @param content
     *          String content to be appended to file.
     * @param encoding
     *          Encoding used for string content.
     * 
     * @throws IOException
     */
    public static void appendContent(String filepath, String content, String encoding) throws IOException {
        appendContent(new File(filepath), content, encoding);
    }

    /**
     * Append text content to file, with encoding.
     * 
     * @param filepath
     *          File to be appended.
     * @param content
     *          String content to be appended to file.
     * @param encoding
     *          Encoding used for string content.
     * 
     * @throws IOException
     */
    public static void appendContent(File file, String content, String encoding) throws IOException {
        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), encoding));
            writer.append(content);
        } finally {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    }
}

Related

  1. appendContentsToFile(File file, StringBuilder contents)
  2. appendContentsTofile(String fileName, String contents)
  3. appendContentToFile(File file, String fileContent)
  4. appendFile(byte[] data, String file)