Advertisement
codeuniv

Serialization

Dec 30th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package wdf;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InvalidObjectException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectInputValidation;
  11. import java.io.ObjectOutputStream;
  12. import java.io.ObjectStreamException;
  13. import java.io.Serializable;
  14.  
  15. class Base implements Serializable {
  16.    
  17. }
  18.  
  19. class Sub extends Base {
  20.     private Sub() {}
  21.     public Sub(int g) {}
  22.    
  23.     int q = 9;
  24. }
  25.  
  26. class Ser extends Sub implements Serializable, ObjectInputValidation {
  27.     Ser() { super(7); }
  28.    
  29.        private void writeObject(java.io.ObjectOutputStream out) throws IOException {
  30.             System.out.println("writeObject");
  31.             out.defaultWriteObject();
  32.         }
  33.        
  34.        private Object writeReplace()  {
  35.             System.out.println("writeReplace");
  36.             return this;
  37.         }
  38.      
  39.     int a = 8;
  40.     float f = 9f;
  41.     String s = "823982";
  42.    
  43.        private Object readResolve() throws ObjectStreamException {
  44.             System.out.println("readResolve");
  45.             return this;
  46.         }
  47.        
  48.        private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
  49.             System.out.println("readObject");
  50.             in.registerValidation(this, 10);
  51.             in.defaultReadObject();
  52.         }
  53.    
  54.    
  55.     public void validateObject() throws InvalidObjectException {
  56.         System.out.println("jjj");
  57. //      if (a != 9) {          
  58. //         
  59. //          throw new InvalidObjectException("a != 9");
  60. //      }
  61.        
  62.     }
  63.    
  64.    
  65. }
  66.  
  67. public class My {
  68.    
  69.  
  70.  
  71.     public static void main(String[] args) {
  72.         String fileName = "d:\\del.me";
  73.         int bufSz = 8 * 1024;
  74.         Ser obj = new Ser();
  75.  
  76.         try {
  77.             ObjectOutputStream oos = new ObjectOutputStream(
  78.                     new BufferedOutputStream(new FileOutputStream(fileName), bufSz));
  79.             oos.writeObject(obj);
  80. //          oos.close();
  81.             oos.flush();
  82.         } catch (IOException e) {
  83.             // TODO Auto-generated catch block
  84.             e.printStackTrace();
  85.         }
  86.  
  87.         Ser obj1 = null;
  88.         Ser obj2 = null;
  89.         try {
  90.             ObjectInputStream ois = new ObjectInputStream(
  91.                     new BufferedInputStream(new FileInputStream(fileName), bufSz));
  92.            
  93.             ObjectInputStream ois1 = new ObjectInputStream(
  94.                     new BufferedInputStream(new FileInputStream(fileName), bufSz));
  95.            
  96.             obj1 = (Ser) ois.readObject();
  97.             obj2 = (Ser) ois1.readObject();
  98.         } catch (IOException | ClassNotFoundException e) {
  99.             // TODO Auto-generated catch block
  100.             e.printStackTrace();
  101.         }
  102.  
  103.         System.out.println(obj1.a);
  104.         System.out.println(obj2.a);
  105.  
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement