Serialized File Util : Serialization « File Input Output « Java






Serialized File Util

        
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//package wekinator.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author rebecca
 */
public class SerializedFileUtil {
    public static Object readFromFile(File f) throws Exception {
        FileInputStream instream = null;
        ObjectInputStream objin = null;
        Object o = null;
        boolean err = false;
        Exception myEx = new Exception();
        try {
            instream = new FileInputStream(f);
            objin = new ObjectInputStream(instream);
            o = objin.readObject();
        } catch (Exception ex) {
            myEx = ex;
            err = true;
            Logger.getLogger(SerializedFileUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (objin != null) {
                    objin.close();
                }
                if (instream != null) {
                    instream.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(SerializedFileUtil.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        if (err) {
            throw myEx;
        }
        return o;
    }

     public static void writeToFile(File f, Object o) throws Exception {
        FileOutputStream outstream = null;
        ObjectOutputStream objout = null;
        boolean success = false;
        Exception myEx = new Exception();
        try {
            outstream = new FileOutputStream(f);
            objout = new ObjectOutputStream(outstream);
            objout.writeObject(o);
            success = true;
        } catch (Exception ex) {
            success = false;
            myEx = new Exception(ex.getMessage());
            Logger.getLogger(SerializedFileUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (objout != null) {
                    objout.close();
                }
                if (outstream != null) {
                    outstream.close();
                }
            } catch (IOException ex) {

                Logger.getLogger(SerializedFileUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (!success)
            throw myEx;

    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Serialization with ObjectInputStream and ObjectOutputStream
2.Object SerializationObject Serialization
3.Serial Demo
4.Create a serialized output file
5.Reconstructing an externalizable objectReconstructing an externalizable object
6.Simple use of Externalizable and a pitfallSimple use of Externalizable and a pitfall
7.Serializable
8.Serializer class
9.This program shows how to use getSerialVersionUID
10.Working with Serialization
11.Assists with the serialization process and performs additional functionality based on serialization.
12.Computes all the class serialVersionUIDs under the jboss home directory.
13.Serializable Enumeration
14.Serialization Utilities
15.This program demonstrates the transfer of serialized objects between virtual machines
16.A class whose clone method uses serialization
17.Writes a serialized version of obj to a given file, compressing it using gzip.
18.Reads a serialized object from a file that has been compressed using gzip.
19.Serializes an object to a file, masking out annoying exceptions.