Serializable:用於儲存及還原對象——《Thinking in Java》隨筆031

來源:互聯網
上載者:User

標籤:pack   tostring   turn   tput   import   對象   port   random   stringbu   

  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 Stream102         try {103             // Output104             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             // Input113             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 stream122             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中超級重要的東西了。

Serializable:用於儲存及還原對象——《Thinking in Java》隨筆031

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.