Java ObjectOutputStream Write saveToFile(String string, File file, boolean append)

Here you can find the source of saveToFile(String string, File file, boolean append)

Description

save To File

License

Open Source License

Declaration

public static void saveToFile(String string, File file, boolean append) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w ww  .  ja  v  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 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. saveSerialized(Serializable obj, File file)
  2. saveStringToFile(String filepath, String content)
  3. saveToFile(File f, Object obj)
  4. saveToFile(final File file, final Object obj)
  5. saveToFile(Serializable object, File file, boolean compress)