Java File Append Text appendToFile(String fileName, String content)

Here you can find the source of appendToFile(String fileName, String content)

Description

append To File

License

Open Source License

Parameter

Parameter Description
fileName The name of file to append to. Must be in data folder.<br/> File will be created if it doesn't exist.
content The content to write

Return

true, if appended successfully, false otherwise

Declaration

public static boolean appendToFile(String fileName, String content) 

Method Source Code


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

import java.io.*;

public class Main {
    /**/*from   w  w w . j a  va  2s  . c  om*/
     *
     * @param fileName    The name of file to append to. Must be in data folder.<br/>
     *                    File will be created if it doesn't exist.
     * @param content     The content to write
     * @return <code>true</code>, if appended successfully, <code>false</code> otherwise
     */
    public static boolean appendToFile(String fileName, String content) {
        return appendToFile(new File(getOutputFolder() + fileName), content);
    }

    /**
     * @param file       The file to append in
     * @param content    The content to write
     * @return <code>true</code>, if appended successfully, <code>false</code> otherwise
     */
    public static boolean appendToFile(File file, String content) {
        try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));) {
            out.println(content);
        } catch (IOException e) {
            System.out.println("Exception while writing to file " + file.getName());
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public static String getOutputFolder() {
        return System.getProperty("user.dir") + "/data/";
    }
}

Related

  1. appendToFile(String baseFilename, String appendingFilename)
  2. appendToFile(String content, File file, String encoding)
  3. appendToFile(String content, String filename)
  4. appendToFile(String contents, String filename)
  5. appendToFile(String file, String line)
  6. appendToFile(String filename, String content, int line)
  7. appendToFile(String filename, String str)
  8. appendToFile(String filename, String text)
  9. appendToFile(String fileName, String text)