Here you can find the source of writeObjectToFile(Object obj, String fileName)
Parameter | Description |
---|---|
obj | The Object to write to a file. |
fileName | The file name to write to. |
public static boolean writeObjectToFile(Object obj, String fileName)
//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)); } }