Subversion Repositories SmartDukaan

Rev

Rev 21 | Rev 42 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.metamodel.util;

import in.shop2020.util.Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author naveen
 *
 */
public class DBUtils {

        /**
         * 
         * @param objectToStore
         * @param dbFile
         * @throws Exception
         */
        public static void store(Object objectToStore, String dbFile) 
                throws Exception {              
                File f = new File(dbFile);
                if(!f.exists()) {
                        f.createNewFile();
                }
                
                FileOutputStream fos = new FileOutputStream(f);
                ObjectOutputStream out = new ObjectOutputStream(fos);
                
                out.writeObject(objectToStore);
                out.close();
                Utils.info("Serialization complete");
        }
        
        /**
         * 
         * @param dbFile
         * @return
         * @throws Exception
         */
        public static Object read(String dbFile) throws Exception {
                File f = new File(dbFile);
                if(!f.exists()) {
                        return null;
                }
                
                FileInputStream fis = new FileInputStream(f);
                ObjectInputStream in = new ObjectInputStream(fis);
                Object obj = in.readObject();
                in.close();
                
                //Utils.info("De-serialization complete");
                return obj;
        }
}