Java Object Save writeObjectToFile(Object o, File file, boolean append)

Here you can find the source of writeObjectToFile(Object o, File file, boolean append)

Description

Write an object to a specified File.

License

Open Source License

Parameter

Parameter Description
o object to be written to file
file The temp File
append If true, append to this file instead of overwriting it

Exception

Parameter Description
IOException If File cannot be written

Return

File containing the object

Declaration

public static File writeObjectToFile(Object o, File file, boolean append) throws IOException 

Method Source Code


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

import java.io.*;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**/*from   ww  w .  j a  va  2s . co m*/
     * Write object to a file with the specified name.
     *
     * @param o
     *          object to be written to file
     * @param filename
     *          name of the temp file
     * @throws IOException
     *           If can't write file.
     * @return File containing the object
     */
    public static File writeObjectToFile(Object o, String filename) throws IOException {
        return writeObjectToFile(o, new File(filename));
    }

    /**
     * Write an object to a specified File.
     *
     * @param o
     *          object to be written to file
     * @param file
     *          The temp File
     * @throws IOException
     *           If File cannot be written
     * @return File containing the object
     */
    public static File writeObjectToFile(Object o, File file) throws IOException {
        return writeObjectToFile(o, file, false);
    }

    /**
     * Write an object to a specified File.
     *
     * @param o
     *          object to be written to file
     * @param file
     *          The temp File
     * @param append If true, append to this file instead of overwriting it
     * @throws IOException
     *           If File cannot be written
     * @return File containing the object
     */
    public static File writeObjectToFile(Object o, File file, boolean append) throws IOException {
        // file.createNewFile(); // cdm may 2005: does nothing needed
        ObjectOutputStream oos = new ObjectOutputStream(
                new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file, append))));
        oos.writeObject(o);
        oos.close();
        return file;
    }
}

Related

  1. writeObject(Object object, String path)
  2. writeObject(Object p_object, File p_outFile)
  3. writeObjectToFile(File file, Object o)
  4. writeObjectToFile(File file, Object obj)
  5. writeObjectToFile(final Object o, final File dir)
  6. writeObjectToFile(Object o, String filename)
  7. writeObjectToFile(Object o, String fileName)
  8. writeObjectToFile(Object obj, File f)
  9. writeObjectToFile(Object obj, String fileName)