Java File Append appendFile(File file, String content)

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

Description

Write some text into the end of a file (append)

License

Open Source License

Parameter

Parameter Description
file File where the text will be written
content Text to be written

Exception

Parameter Description
FileNotFoundException If the file is a directory
IOException If an I/O error happend while writing

Declaration

public static void appendFile(File file, String content) throws FileNotFoundException, IOException 

Method Source Code

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

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    /**/*  w  w  w.j av  a2 s .c om*/
     * Write some text into the end of a file (append)
     * @param file File where the text will be written
     * @param content Text to be written
     * @throws FileNotFoundException If the file is a directory
     * @throws IOException If an I/O error happend while writing
     */
    public static void appendFile(File file, String content) throws FileNotFoundException, IOException {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file, true));
            bw.write(content);
            bw.flush();
        } catch (FileNotFoundException e) {
            if (bw != null)
                bw.close();
            throw e;
        } finally {
            if (bw != null)
                bw.close();
        }
    }
}

Related

  1. appendContentsTofile(String fileName, String contents)
  2. appendContentToFile(File file, String fileContent)
  3. appendFile(byte[] data, String file)
  4. appendFile(File f, String content)
  5. appendFile(File f, StringBuffer sb)
  6. appendFile(File file, String url)
  7. appendFile(File filename, String data)
  8. appendFile(File src, File dst)
  9. appendFile(File targetFile, String text)