Here you can find the source of serialize(Serializable object, File dest)
Parameter | Description |
---|---|
object | the object to serialized. |
dest | the file to write. |
Parameter | Description |
---|---|
IOException | an exception |
public static void serialize(Serializable object, File dest) throws IOException
//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); } } } } }