File Serialization

  1. import java.io.FileOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.ObjectOutputStream;  
  4.   
  5. public class SERIALIZATION_INTERFACE {  
  6.   
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.         Employee employee=new Employee();  
  10.         employee.id=10;  
  11.         employee.name="Adam";  
  12.           
  13.         try {  
  14.             FileOutputStream fOutputStream=new FileOutputStream("G:/employee.ser");  
  15.             ObjectOutputStream outputStream=new ObjectOutputStream(fOutputStream);  
  16.             outputStream.writeObject(employee);  
  17.             outputStream.flush();  
  18.             outputStream.close();  
  19.             fOutputStream.close();  
  20.             System.out.println("Data saved");  
  21.         } catch (IOException e) {  
  22.             // TODO: handle exception  
  23.             e.printStackTrace();  
  24.         }  
  25.   
  26.     }  
  27.   
  28. }  
  29.   
  30.   
  31.   
  32.   
  33. import java.io.Serializable;  
  34.   
  35. public class Employee implements Serializable {  
  36.       
  37.     transient public int id;  
  38.     transient public String name;  
  39.       
  40.     public void getName(){  
  41.         System.out.println("Employee name : "+name);  
  42.     }  
  43.       
  44.     public void getId() {  
  45.         System.out.println("Employee id : "+id);  
  46.     }  
  47.   
  48. }  
Share:

0 Comments:

Post a Comment