Deserialize an object from file - Java File Path IO

Java examples for File Path IO:Serialization

Description

Deserialize an object from file

Demo Code


import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.logging.*;

public class Main{
    /**/*from   w ww . j  a va 2 s  . c  o  m*/
     * Deserialize an object from file
     * @param filename The filename of the serialized object
     * @return The deserialized object
     */
    public static Object deserialize(String filename) {
        System.out.println("Deserializing " + filename);
        Object object = null;

        try {
            FileInputStream fileIn = new FileInputStream(filename);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            object = in.readObject();
            in.close();
            fileIn.close();
        } catch (IOException i) {
            Logger.getLogger(HelperFunctions.class.getName()).log(
                    Level.SEVERE, null, i);
        } catch (ClassNotFoundException c) {
            Logger.getLogger(HelperFunctions.class.getName()).log(
                    Level.SEVERE, null, c);
        }

        System.out.println("Done deserializing " + filename);
        return object;
    }
}

Related Tutorials