Serializes an object to a file, masking out annoying exceptions. : Serialization « File Input Output « Java

Serializes an object to a file, masking out annoying exceptions.

/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept.
   This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
   This software is provided under the terms of the Common Public License,
   version 1.0, as published by  For further
   information, see the file `LICENSE' included with this distribution. */

//package cc.mallet.util;


 * @author <a href="">Charles Sutton</a>
 * @version $Id:,v 1.1 2007/10/22 21:37:40 mccallum Exp $
public class Util {

     * Serializes an object to a file, masking out annoying exceptions.
     *  Any IO exceptions are caught, and printed to standard error.
     *  Consider using {@link #writeGzippedObject(,}
     *  instead, for that method will compress the serialized file, and it'll still
     * be reaoable by {@link #readObject}.
     * @param f File to write to
     * @param obj Object to serialize
     * @see #writeGzippedObject(,
    public static void writeObject (File f, Serializable obj)
      try {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
      catch (IOException e) {
        System.err.println("Exception writing file " + f + ": " + e);


Related examples in the same category

1.Serialization with ObjectInputStream and ObjectOutputStream
2.Object SerializationObject Serialization
3.Serial Demo
4.Create a serialized output file
5.Reconstructing an externalizable objectReconstructing an externalizable object
6.Simple use of Externalizable and a pitfallSimple use of Externalizable and a pitfall
8.Serializer class
9.This program shows how to use getSerialVersionUID
10.Working with Serialization
11.Assists with the serialization process and performs additional functionality based on serialization.
12.Computes all the class serialVersionUIDs under the jboss home directory.
13.Serializable Enumeration
14.Serialization Utilities
15.This program demonstrates the transfer of serialized objects between virtual machines
16.A class whose clone method uses serialization
17.Writes a serialized version of obj to a given file, compressing it using gzip.
18.Reads a serialized object from a file that has been compressed using gzip.
19.Serialized File Util