Thursday, December 7, 2017

Serialization,Externalizable









Serialization


 class Student implements Serializable {  
      int id;  String name;  String lastname;  
      private static final long serialVersionUID = -8000090944414208496L;  
      public Student(){}  
   //getter setters.........
 }  
 public class SerializableExample {  
      // deserialize to Object from given file  
      public static Object deserialize(String fileName) throws IOException,  
                ClassNotFoundException {  
           FileInputStream fis = new FileInputStream(fileName);  
           ObjectInputStream ois = new ObjectInputStream(fis);  
           Object obj = ois.readObject();  
           ois.close();  
           return obj;  
      }  
      // serialize the given object and save it to file  
      public static void serialize(Object obj, String fileName)  
                throws IOException {  
           FileOutputStream fos = new FileOutputStream(fileName);  
           ObjectOutputStream oos = new ObjectOutputStream(fos);  
           oos.writeObject(obj);  
           fos.close();  
      }  
      public static void main(String[] args) {  
           /*     Student student = new Student();  
           student.setId(1);  
           student.setName("aaaaa");  
           student.setLastname("bbbbbbb");  
           try {  
                serialize(student,"student.txt");  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      */       
           String fileName="student.txt";  
           Student student = null;  
           try {  
                student = (Student) deserialize(fileName);  
                System.out.println(student.getName());  
           } catch (ClassNotFoundException | IOException e) {  
                e.printStackTrace();  
           }  
      }  
 } 

Externalizable  : For security ,helps make data secure

 
 class Employee implements Externalizable{  
      int id;  
      String name;  
      String lastname;  
      public Employee(){}  
      @Override  
      public void writeExternal(java.io.ObjectOutput out) throws IOException {  
           out.writeInt(id);  
           out.writeObject("aa"+name);  
      }  
      @Override  
      public void readExternal(java.io.ObjectInput in) throws IOException, ClassNotFoundException {  
           id = in.readInt();  
           if(name!=null){  
                name = (String) in.readObject();  
                //if(!name.startsWith("aa"))throw new IOException("curropt data");  
           //     name = name.substring(2);  
           }  
      }  
//getter setters
 }  
 public class ExternalizableExample {  
                public static void main(String args[]){  
                     Employee employee = new Employee();  
                     employee.setId(1);  
                     employee.setName("aaaaa");  
                     //serializeObject( employee);  
                     deSerializeObject("");  
                }  
                public static void serializeObject(Employee employee){  
                     try {  
                            FileOutputStream output = new FileOutputStream("employee.txt");  
                            ObjectOutputStream oos = new ObjectOutputStream(output);  
                            oos.writeObject(employee);  
                            oos.close();  
                     } catch (IOException e) {  
                               e.printStackTrace();  
                     }       
                }  
                public static void deSerializeObject(String filename){  
                     filename = "employee.txt";  
                     FileInputStream input;  
                     try {  
                          input = new FileInputStream(filename);  
                          ObjectInputStream ois = new ObjectInputStream(input);  
                       Employee employee = (Employee)ois.readObject();  
                       ois.close();  
                       System.out.println("Employee id ="+employee.getId());  
                       System.out.println("Employee name is ="+employee.getName());  
                     } catch (IOException | ClassNotFoundException e) {  
                          e.printStackTrace();  
                     }   }  }  








No comments:

Post a Comment