Java File Append Text appendToFile(String string, File file)

Here you can find the source of appendToFile(String string, File file)

Description

append To File

License

Open Source License

Declaration

public static void appendToFile(String string, File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   ww  w  .jav  a 2s  .c o m*/
Open Auto Trading : A fully automatic equities trading platform with machine learning capabilities
Copyright (C) 2015 AnyObject Ltd.
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program 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 General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

public class Main {
    public static void appendToFile(String string, File file) throws IOException {
        saveToFile(string, file, true);
    }

    public static void saveToFile(String string, File file, boolean append) throws IOException {
        FileWriter fw = null;

        try {
            fw = new FileWriter(file, append);

            PrintWriter out = new PrintWriter(fw);

            out.print(string);
            out.close();

        } catch (IOException ex) {
            throw ex;
        } finally {
            try {
                fw.close();
            } catch (IOException ex) {
                throw ex;
            }
        }
    }

    public static void saveToFile(String string, File file) throws IOException {
        saveToFile(string, file, false);
    }

    public static String saveToFile(Serializable object, File file) throws IOException {
        store(object, file);
        return object.toString() + " saved at " + file.toString();
    }

    /**
     * Serialise the object o (and any serialisable objects it refers to) and
     * store its serialised state in File f.
     *
     * @param o
     * @param file
     * @throws IOException
     */
    public static void store(Serializable o, File file) throws IOException {
        ObjectOutputStream out = // The class for serialization
                new ObjectOutputStream(new FileOutputStream(file));

        try {
            out.writeObject(o);
        } catch (IOException ex) {
            throw ex;
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                throw ex;
            }
        }
    }
}

Related

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