File Deserialization

  1. import java.io.FileInputStream;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.IOException;  
  4. import java.io.ObjectInputStream;  
  5.   
  6. public class DeserializeDemo {  
  7.   
  8.     public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException {  
  9.         // TODO Auto-generated method stub  
  10.           
  11.         Employee employee;  
  12.         FileInputStream fStream=new FileInputStream("G:/employee.ser");  
  13.         try {  
  14.             ObjectInputStream oStream=new ObjectInputStream(fStream);  
  15.             employee=(Employee)oStream.readObject();  
  16.               
  17.             //System.out.println(employee.id);  
  18.             //System.out.println(employee.name);  
  19.             //System.out.println(employee.getName());  
  20.             employee.getId();  
  21.             employee.getName();  
  22.         } catch (IOException e) {  
  23.             // TODO Auto-generated catch block  
  24.             e.printStackTrace();  
  25.         }  
  26.           
  27.           
  28.   
  29.     }  
  30.   
  31. }  
  32.   
  33.   
  34.   
  35.   
  36. import java.io.Serializable;  
  37.   
  38. public class Employee implements Serializable {  
  39.       
  40.   
  41.         //public int id;  
  42.         //public String name;  
  43.   
  44.     transient public int id;  //When you don't use transient you get value.....
  45.     transient public String name;  
  46.       
  47.     public void getName(){  
  48.         System.out.println("Employee name : "+name);  
  49.     }  
  50.       
  51.     public void getId() {  
  52.         System.out.println("Employee id : "+id);  
  53.     }  
  54.   
  55. }  
Share:

0 Comments:

Post a Comment