Java Object Serialize serialize(Serializable object, File dest)

Here you can find the source of serialize(Serializable object, File dest)

Description

Serialize and compress an object to the file path provided.

License

Open Source License

Parameter

Parameter Description
object the object to serialized.
dest the file to write.

Exception

Parameter Description
IOException an exception

Declaration

public static void serialize(Serializable object, File dest) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w  w w .j  a v a  2  s . c om*/
 * This software is released as part of the Pumpernickel project.
 * 
 * All com.pump resources in the Pumpernickel project are distributed under the
 * MIT License:
 * https://raw.githubusercontent.com/mickleness/pumpernickel/master/License.txt
 * 
 * More information about the Pumpernickel project is available here:
 * https://mickleness.github.io/pumpernickel/
 */

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**
     * Serialize and compress an object to the file path provided.
     * 
     * See {@link #deserialize(File)}.
     * 
     * @param object
     *            the object to serialized.
     * @param dest
     *            the file to write.
     * @throws IOException
     */
    public static void serialize(Serializable object, File dest) throws IOException {

        if (!dest.exists()) {
            if (!dest.createNewFile()) {
                throw new IOException("File.createNewFile() failed for " + dest.getAbsolutePath());
            }
        }

        try (FileOutputStream fileOut = new FileOutputStream(dest)) {
            try (GZIPOutputStream zipOut = new GZIPOutputStream(fileOut)) {
                try (ObjectOutputStream objOut = new ObjectOutputStream(zipOut)) {
                    objOut.writeObject(object);
                }
            }
        }
    }
}

Related

  1. serialize(Serializable obj, String path)
  2. serialize(Serializable object)
  3. serialize(Serializable object)
  4. serialize(Serializable object)
  5. serialize(Serializable object)
  6. serialize(Serializable object, String fileName)
  7. serialize(Serializable s, File dir, String fileName)
  8. serialize(Serializable serializable)
  9. serialize(Serializable... objects)