Java Object Save writeObjectToFile(Object obj, String fileName)

Here you can find the source of writeObjectToFile(Object obj, String fileName)

Description

Writes an Object to a file.

License

Open Source License

Parameter

Parameter Description
obj The Object to write to a file.
fileName The file name to write to.

Return

true if the write succeeded, false otherwise.

Declaration

public static boolean writeObjectToFile(Object obj, String fileName) 

Method Source Code


//package com.java2s;
/*/*  w w  w.ja  va  2  s .  c  o m*/
 * File:  SerializeUtil.java
 *
 * Copyright (C) 2003, Chris M. Bouzek
 *
 * 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 2
 * 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact : Dennis Mikkelson <mikkelsond@uwstout.edu>
 *           Chris Bouzek <coldfusion78@yahoo.com>
 *           Department of Mathematics, Statistics and Computer Science
 *           University of Wisconsin-Stout
 *           Menomonie, WI 54751, USA
 *
 * This work was supported by the National Science Foundation under grant
 * number DMR-0218882.
 *
 * For further information, see <http://www.pns.anl.gov/ISAW/>
 *
 * $Log$
 * Revision 1.2  2004/03/11 22:17:04  millermi
 * - Changed package names and replaced SharedData with
 *   SharedMessages class.
 *
 * Revision 1.1  2003/10/30 02:10:47  bouzekc
 * Added to CVS.
 *
 *
 */

import java.io.*;

public class Main {
    /**
     * Writes an Object to a file.  This will overwrite anything currently in the
     * File.  It also automatically closes the OutputStream and handles
     * Exceptions.
     *
     * @param obj The Object to write to a file.
     * @param file The file to write to.
     *
     * @return true if the write succeeded, false otherwise.
     */
    public static boolean writeObjectToFile(Object obj, File file) {
        ObjectOutputStream out = null;

        try {
            out = new ObjectOutputStream(new FileOutputStream(file));
            out.writeObject(obj);

            return true;
        } catch (IOException ioe) {
            return false;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ioe) {
                    //drop it on the floor
                }
            }
        }
    }

    /**
     * Writes an Object to a file.  This will overwrite anything currently in the
     * File.  It also automatically closes the OutputStream and handles
     * Exceptions.
     *
     * @param obj The Object to write to a file.
     * @param fileName The file name to write to.
     *
     * @return true if the write succeeded, false otherwise.
     */
    public static boolean writeObjectToFile(Object obj, String fileName) {
        return writeObjectToFile(obj, new File(fileName));
    }
}

Related

  1. writeObjectToFile(final Object o, final File dir)
  2. writeObjectToFile(Object o, File file, boolean append)
  3. writeObjectToFile(Object o, String filename)
  4. writeObjectToFile(Object o, String fileName)
  5. writeObjectToFile(Object obj, File f)
  6. writeObjectToFile(Object obj, String path)
  7. writeObjectToFile(Object object, File file)
  8. writeObjectToFile(Object oObject, File destDir, String filename)
  9. writeObjectToFile(String dirname, String filename, Object obj)