Rahmadnet

Java Serialization

Jan 29th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. 1. write an essay about what is Serialization, define some cases and select appropiate
  2.     Serialization scenario to solve those case, explain why.
  3.     Serialization adalah mekanisme mengubah keadaan suatu objek menjad
  4.     aliran byte.
  5.     1. Untuk menyimpan / mempertahankan status suatu objek
  6.     2. Untuk melakukan perjalanan objek di jaringan.
  7. 2. write a simple code to show Serialization in action by implementing you cases above.
  8.  
  9.     import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.ObjectInputStream;
  13. import java.io.ObjectOutputStream;
  14. public class reliasasi implements java.io.Serializable
  15. {
  16.     public int a;
  17.     public String b;
  18.    
  19.     // Default Constructor
  20.    
  21.     public reliasasi(int a, String b)
  22.     {
  23.         this.a = a;
  24.         this.b = b;
  25.     }
  26. }
  27.    
  28.  
  29. import java.io.FileInputStream;
  30. import java.io.FileOutputStream;
  31. import java.io.IOException;
  32. import java.io.ObjectInputStream;
  33. import java.io.ObjectOutputStream;
  34.  
  35. class test
  36.     {
  37.         public static void main(String[] args)
  38.         {
  39.            
  40.             reliasasi k = new reliasasi(1, "Rahmad");
  41.             String filenama = "file.ser";
  42.            
  43.             // Serialization
  44.             try
  45.             {
  46.                 // Saving of object in a file
  47.                 FileOutputStream file = new FileOutputStream(filenama);
  48.                 ObjectOutputStream out = new ObjectOutputStream(file);
  49.                
  50.                 // Method for serialization of object
  51.                 out.writeObject(k);
  52.                
  53.                 out.close();
  54.                 file.close();
  55.                 System.out.println("Object has been serialized");
  56.             }
  57.            
  58.             catch(IOException ex)
  59.             {
  60.                 System.out.println("IOException is caught");
  61.             }
  62.            
  63.             reliasasi k1 = null;
  64.            
  65.             // Deserialzation
  66.             try
  67.             {
  68.                 // Reading the object from a file
  69.                 FileInputStream file = new FileInputStream(filenama);
  70.                 ObjectInputStream in = new ObjectInputStream(file);
  71.                
  72.                 // Method for deserialization of object
  73.                 k1 = (reliasasi) in.readObject();
  74.                
  75.                 in.close();
  76.                 file.close();
  77.                
  78.                 System.out.println("Object has been deserialized");
  79.                 System.out.println("Result a = " + k1.a);
  80.                 System.out.println("Result b = " + k1.b);
  81.                
  82.             }
  83.            
  84.             catch (IOException ex)
  85.             {
  86.                 System.out.println("IOException is caught");
  87.             }
  88.            
  89.             catch (ClassNotFoundException ex)
  90.             {
  91.                 System.out.println("ClassNotFoundException is caught");
  92.             }
  93.         }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment