Here you can find the source of serialize(Serializable object, String fileName)
Parameter | Description |
---|---|
object | a parameter |
fileName | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void serialize(Serializable object, String fileName) throws IOException
//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(); } }