• Breaking News

    Monday 1 September 2014

    How make java object Serialzable

    If we want to make object to be serializeble then class of that object must implements serializable interface. Serialzable interface is marker interface.To serialze a object we convert them into byte stream. and we de-serialize that object from that stream.

    Employee.java


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import java.io.Serializable;
    
    public class Employee implements Serializable{
    
     private String name;
     private float salary;
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public float getSalary() {
      return salary;
     }
     public void setSalary(float salary) {
      this.salary = salary;
     } 
    }
    

    SerialzableExample.java

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    
    public class SerializableExample {
    
     ObjectOutputStream oos=null;
     
     FileOutputStream fos=null;
     
     FileInputStream fis=null;
     
     ObjectInputStream ois=null;
     
     public static void main(String[] args) {
      
      SerializableExample serial=new SerializableExample();
      
      /*
       * write employee object into file
       */
      serial.employeeSerialzable();
      
      /*
       * read employee object from file
       */
      serial.employeeDeSerialzable();
     }
    
     
    
     private void employeeSerialzable() {
    
      Employee emp=new Employee();//create new java object
      emp.setName("Arpit Sharma");//set name of employee
      emp.setSalary(100000);//set salary of employee
     
     
      try {
       fos=new FileOutputStream("Demo.txt");//open demo.txt to write object into them
       oos=new ObjectOutputStream(fos);
       oos.writeObject(emp);//serialize object
    
      } catch (IOException e) {
       
       e.printStackTrace();
      }
      
     }
     
     private void employeeDeSerialzable() {
      try {
       fis=new FileInputStream("Demo.txt");//read demo.txt to read object
       ois=new ObjectInputStream(fis);
       Employee emp=(Employee) ois.readObject();//deserialize object 
       System.out.println("Employee Name: "+emp.getName());
       System.out.println("Employee Salary: "+emp.getSalary());
      } catch (IOException | ClassNotFoundException e) {
       
       e.printStackTrace();
      }
      
      
     }
     
    }
    

    No comments:

    Post a Comment