Java File Append Text appendToFile(String str, String filename, boolean appendNewLine)

Here you can find the source of appendToFile(String str, String filename, boolean appendNewLine)

Description

Appends the string to the specified file.

License

Open Source License

Declaration

public static void appendToFile(String str, String filename, boolean appendNewLine) throws IOException 

Method Source Code


//package com.java2s;
/* This file is part of the Joshua Machine Translation System.
 * /*from  w  ww  .  j  ava 2  s.co  m*/
 * Joshua is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

import java.io.*;

public class Main {
    public static final String FILE_ENCODING = "utf8";

    /** 
     * Appends the string to the specified file.
     */
    public static void appendToFile(String str, String filename, boolean appendNewLine) throws IOException {
        BufferedWriter writer = getBufferedWriter(filename, true);
        writer.write(str, 0, str.length());
        if (appendNewLine)
            writer.newLine();
        writer.flush();
        writer.close();
    }

    /**
     * Returns a BufferedWriter.  Overwrites the file if it exists.
     * 
     * @param filename the name of the file to write to
     * @param the directory to put the file is in
     * @return BufferedReader to read from the given file name 
     * @exception IOException if a stream cannot be created
     */
    public static BufferedWriter getBufferedWriter(String directory, String filename) throws IOException {
        return getBufferedWriter(directory, filename, false);
    }

    /**
     * Returns a BufferedWriter. Overwrites the file if it exists.
     * 
     * @param filename the name of the file to write to
     * @return BufferedReader to read from the given file name 
     * @exception IOException if a stream cannot be created
     */
    public static BufferedWriter getBufferedWriter(String filename) throws IOException {
        return getBufferedWriter(filename, false);
    }

    /**
     * Returns a BufferedWriter.  Can optionally append ot the file if it exists.
     * 
     * @param filename the name of the file to write to
     * @param the directory to put the file is in
     * @param append a flag to determine whether to append to an existing file or overwrite it
     * @return BufferedReader to read from the given file name 
     * @exception IOException if a stream cannot be created
     */
    public static BufferedWriter getBufferedWriter(String directory, String filename, boolean append)
            throws IOException {
        File file = new File(directory, filename);
        FileOutputStream outputStream = new FileOutputStream(file, append);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, FILE_ENCODING);
        return new BufferedWriter(outputStreamWriter);
    }

    /**
     * Returns a BufferedWriter. Can optionally append ot the file if it exists.
     * 
     * @param filename the name of the file to write to
     * @param append a flag to determine whether to append to an existing file or overwrite it
     * @return BufferedReader to read from the given file name 
     * @exception IOException if a stream cannot be created
     */
    public static BufferedWriter getBufferedWriter(String filename, boolean append) throws IOException {
        FileOutputStream outputStream = new FileOutputStream(filename, append);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, FILE_ENCODING);
        return new BufferedWriter(outputStreamWriter);
    }

    /**
     * Writes a line to a BufferedWriter without a terminating newline character.
     */
    public static void write(String line, BufferedWriter writer) throws IOException {
        writer.write(line, 0, line.length());
        writer.flush();
    }
}

Related

  1. appendToFile(String filename, String text)
  2. appendToFile(String filePath, String content)
  3. appendToFile(String filePath, String data)
  4. appendToFile(String filepath, String newLine)
  5. appendToFile(String sb, String directory, String fileName)
  6. appendToFile(String string, File file)
  7. appendToFile(String text, String fileName)
  8. appendToString(final byte b, final Appendable ap)
  9. appendToTextFile(final File file, final String text)