Friday 24 February 2012

Deexternalization in java


package com.usr.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeExternalizationDemo {
       public static void main(String[] args) {
              Employee emp = new Employee();
              File file = new File("employee.ser");
              FileInputStream fis = null;
              ObjectInputStream ois = null;
              try {
                     fis = new FileInputStream(file);
                     ois = new ObjectInputStream(fis);
                     emp.readExternal(ois);
              } catch (IOException ex) {
                     ex.printStackTrace();
              } catch (ClassNotFoundException ex) {
                     ex.printStackTrace();
              } finally {
                     try {
                           if (ois != null) {
                                  ois.close();
                           }
                     } catch (IOException ex) {
                           ex.printStackTrace();
                     }
                     try {
                           if (fis != null) {
                                  fis.close();
                           }
                     } catch (IOException ex) {
                           ex.printStackTrace();
                     }

              }
              System.out.println("Object DeExternalized........");
              System.out.println(emp.name);
              System.out.println(emp.age);
              System.out.println(emp.salary);

       }
}

output 
Object DeExternalized........
name:Alex
age:26
salary:34567.8