Serializable:用于保存及还原对象——《Thinking in Java》随笔031

  1 //: Worm.java
  2 package c10;
  3 
  4 import java.io.ByteArrayInputStream;
  5 import java.io.ByteArrayOutputStream;
  6 import java.io.FileInputStream;
  7 import java.io.FileOutputStream;
  8 import java.io.ObjectInputStream;
  9 import java.io.ObjectOutputStream;
 10 import java.io.Serializable;
 11 
 12 /**
 13 *    @time:         上午10:35:14
 14 *    @date:         2017年5月1日
 15 *    @auther:    skyfffire
 16 *    @version:    v0.1
 17 */
 18 class Data implements Serializable {
 19     private static final long serialVersionUID = 
 20             -1235946666506705202L;
 21     
 22     private int i;
 23     
 24     Data(int x) {
 25         i = x;
 26     }
 27     
 28     public String toString() {
 29         return Integer.toString(i);
 30     }
 31 }
 32 
 33 public class Worm implements Serializable {
 34     private static final long serialVersionUID = 
 35             7755684981500799364L;
 36     
 37     private static int r() {
 38         return (int)(Math.random() * 10);
 39     }
 40     
 41     private Data[] d = 
 42         {new Data(r()), new Data(r()), new Data(r())};
 43     private Worm next = null;
 44     private char c;
 45     
 46     Worm(int i, char x) {
 47         System.out.println("Worm constructor:" + i);
 48         
 49         c = x;
 50         
 51         if (--i > 0) {
 52             next = new Worm(i, (char)(x + 1));
 53         }
 54     }
 55     
 56     Worm() {
 57         System.out.println("Default constructor.");
 58     }
 59     
 60     public String toString() {
 61         StringBuffer s = new StringBuffer(" " + c + "(");
 62         
 63         for (int i = 0; i < d.length; i++) {
 64             s.append(d[i].toString());
 65         }
 66         
 67         s.append(')');
 68         
 69         if (next != null) {
 70             s.append(next.toString());
 71         }
 72         
 73         return s.toString();
 74     }
 75     
 76     public static void main(String[] args) {
 77         Worm w = new Worm(6, 'a');
 78         
 79         System.out.println("w = " + w);
 80         
 81         // Object IO Stream
 82         try {
 83             // Output
 84             ObjectOutputStream out = new ObjectOutputStream(
 85                     new FileOutputStream("worm.out"));
 86             out.writeObject("Worm storage");
 87             out.writeObject(w);
 88             out.close();
 89             
 90             // Input
 91             ObjectInputStream in = new ObjectInputStream(
 92                     new FileInputStream("worm.out"));
 93             String s = (String)in.readObject();
 94             Worm w2 = (Worm)in.readObject();
 95             System.out.println(s + ", w2 = " + w2);
 96             in.close();
 97         } catch (Exception e) {
 98             e.printStackTrace();
 99         }
100         
101         // Byte IO Stream
102         try {
103             // Output
104             ByteArrayOutputStream bout = 
105                     new ByteArrayOutputStream();
106             ObjectOutputStream out = 
107                     new ObjectOutputStream(bout);
108             out.writeObject("Worm storage");
109             out.writeObject(w);
110             out.flush();
111             
112             // Input
113             ByteArrayInputStream bin = 
114                     new ByteArrayInputStream(bout.toByteArray());
115             ObjectInputStream in = 
116                     new ObjectInputStream(bin);
117             String s = (String)in.readObject();
118             Worm w3 = (Worm)in.readObject();
119             System.out.println(s + ", w3 = " + w3);
120             
121             // close IO stream
122             in.close();
123             out.close();
124         } catch (Exception e) {
125             e.printStackTrace();
126         }
127     }
128 }
129 
130 ///:~

输出结果:

Worm constructor:6
Worm constructor:5
Worm constructor:4
Worm constructor:3
Worm constructor:2
Worm constructor:1
w = a(842) b(627) c(504) d(753) e(709) f(651)
Worm storage, w2 = a(842) b(627) c(504) d(753) e(709) f(651)
Worm storage, w3 = a(842) b(627) c(504) d(753) e(709) f(651)

可以看到,实现了Serializable接口的类可以将对象转换为数据流并还原。

序列化(Serializable)可以说是Java中超级重要的东西了。

原文地址:https://www.cnblogs.com/skyfffire/p/6791593.html