ObjectSerializer to store object in file

| Sunday, December 27, 2009
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;


public class ObjectSerializer {

   public ObjectSerializer() {
   }

   public boolean save(Object o, String fileName) {
       try {
           FileOutputStream f = new FileOutputStream(fileName);
           ObjectOutputStream s = new ObjectOutputStream(f);
           s.writeObject(o);
           s.flush();
           s.close();
           f.close();
           return true;
       } catch (IOException ex) {
           ;
           return false;
       }
   }

   public Object load(String fileName) {
       Object o = null;
       try {
           FileInputStream f = new FileInputStream(fileName);
           ObjectInputStream s = new ObjectInputStream(f);
           o = s.readObject();
       } catch (Exception ex) {
           
       }
       return o;
   }
}

0 comments:

Post a Comment