Java Object Save writeObjectToFile(final Object o, final File dir)

Here you can find the source of writeObjectToFile(final Object o, final File dir)

Description

Utility function to serialize an object to a file in current checkpoint dir.

License

Open Source License

Parameter

Parameter Description
o Object to serialize.
dir Directory to serialize into.

Exception

Parameter Description
IOException an exception

Declaration

public static void writeObjectToFile(final Object o, final File dir)
        throws IOException 

Method Source Code

//package com.java2s;
/* CheckpointUtils/*  w w  w . j  a va2s. c o m*/
 *
 * $Id: CheckpointUtils.java 4658 2006-09-25 22:40:19Z paul_jack $
 *
 * Created on December 16, 2005.
 *
 * Copyright (C) 2005 Internet Archive.
 *
 * This file is part of the Heritrix web crawler (crawler.archive.org).
 *
 * Heritrix is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * any later version.
 *
 * Heritrix 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 Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with Heritrix; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

public class Main {
    public static final String SERIALIZED_CLASS_SUFFIX = ".serialized";

    /**
     * Utility function to serialize an object to a file in current checkpoint
     * dir. Facilities
     * to store related files alongside the serialized object in a directory
     * named with a <code>.auxillary</code> suffix.
     *
     * @param o Object to serialize.
     * @param dir Directory to serialize into.
     * @throws IOException
     */
    public static void writeObjectToFile(final Object o, final File dir)
            throws IOException {
        writeObjectToFile(o, null, dir);
    }

    public static void writeObjectToFile(final Object o,
            final String suffix, final File dir) throws IOException {
        dir.mkdirs();
        ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream(getClassCheckpointFile(dir, suffix,
                        o.getClass())));
        try {
            out.writeObject(o);
        } finally {
            out.close();
        }
    }

    public static File getClassCheckpointFile(File checkpointDir,
            final String suffix, Class c) {
        return new File(checkpointDir,
                getClassCheckpointFilename(c, suffix));
    }

    public static File getClassCheckpointFile(File checkpointDir, Class c) {
        return new File(checkpointDir, getClassCheckpointFilename(c, null));
    }

    public static String getClassCheckpointFilename(final Class c) {
        return getClassCheckpointFilename(c, null);
    }

    public static String getClassCheckpointFilename(final Class c,
            final String suffix) {
        return c.getName() + ((suffix == null) ? "" : "." + suffix)
                + SERIALIZED_CLASS_SUFFIX;
    }
}

Related

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