Java Object Serialize serialize(Serializable object, String fileName)

Here you can find the source of serialize(Serializable object, String fileName)

Description

Uses compression

License

Apache License

Parameter

Parameter Description
object a parameter
fileName a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void serialize(Serializable object, String fileName) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedOutputStream;

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

import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**//from   ww w.  j ava  2  s .com
     * Uses compression
     * 
     * @param object
     * @param fileName
     * @throws IOException
     */
    public static void serialize(Serializable object, String fileName) throws IOException {
        serialize(object, fileName, true);
    }

    /**
     * Convenience method used to serialize and write-out objects to a file.
     * 
     * @param object       object to serialize
     * @param fileName       the filename to serialize to
     * @throws IOException    if there is a problem during serialization
     */
    public static void serialize(Serializable object, String fileName, boolean compress) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
        serialize(object, bos, compress);
    }

    /**
     * Uses compression
     * 
     * @param object
     * @param os
     * @throws IOException
     */
    public static void serialize(Serializable object, OutputStream os) throws IOException {
        serialize(object, os, true);
    }

    /**
     * Convenience method used to serialize and write-out objects to an arbitrary output stream.
     * Note: this method does NOT wrap the output stream with a BufferedOutputStream.
     * 
     * @param object       object to serialize
     * @param os          the output stream to serialize to
     * @param compress      whether or not to compress the output stream
     * @throws IOException    if there is a problem during serialization
     */
    private static void serialize(Serializable object, OutputStream os, boolean compress) throws IOException {

        ObjectOutputStream oos;
        if (compress) {
            oos = new ObjectOutputStream(new GZIPOutputStream(os));
        } else {
            oos = new ObjectOutputStream(os);
        }
        oos.writeObject(object);
        oos.flush();
        oos.close();
    }
}

Related

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