1. 程式人生 > >Java編程思想:序列化基礎部分

Java編程思想:序列化基礎部分

ngs 序列 stack @override zab 輸出 tint serial over

  1 import java.io.*;
  2 import java.util.Date;
  3 import java.util.Random;
  4 
  5 public class Test {
  6     public static void main(String[] args){
  7 //        Worm.test();
  8 //        Blip.test();
  9 //        Login.test();
 10         SerialCtl.test();
 11     }
 12 }
 13 
 14 
 15
class Data implements Serializable { 16 private int n; 17 18 public Data(int n) { 19 this.n = n; 20 } 21 22 public String toString() { 23 return Integer.toString(n); 24 } 25 } 26 27 class Worm implements Serializable { 28 private static
final int SIZE = 1024; 29 private static Random random = new Random(47); 30 private Data[] datas = new Data[]{ 31 new Data(random.nextInt(10)), 32 new Data(random.nextInt(10)), 33 new Data(random.nextInt(10)) 34 }; 35 36 private Worm next;
37 private char c; 38 39 public Worm(int i, char c) { 40 System.out.println("Worm Constructor: " + i); 41 this.c=c; 42 if (--i > 0) { 43 next=new Worm(i,(char)(c+1)); 44 } 45 } 46 47 public Worm() { 48 System.out.println("Default Constructor"); 49 } 50 51 public String toString() { 52 StringBuilder sb = new StringBuilder(":"); 53 sb.append("c"); 54 sb.append("("); 55 for (Data data : datas) { 56 sb.append(data); 57 } 58 sb.append(")"); 59 if (next != null) { 60 sb.append(next); 61 } 62 return sb.toString(); 63 } 64 65 public static void test() { 66 try{ 67 Worm w = new Worm(6, ‘a‘); 68 System.out.println("w = "+w); 69 70 /* 71 72 //創建ObjectOutputStream用於儲存序列化結果 73 ObjectOutputStream out = new ObjectOutputStream( 74 new FileOutputStream("worm.out")); 75 76 out.writeObject("Worm Storage\n"); 77 out.writeObject(w); 78 out.close(); 79 80 81 //創建ObjectInputStream用於讀取序列化數據 82 ObjectInputStream in = new ObjectInputStream( 83 new FileInputStream("worm.out")); 84 85 //從序列化數據恢復時,需要按照序列化順序 86 String s = (String)in.readObject(); 87 Worm w2 = (Worm)in.readObject(); 88 System.out.println(s+"w2 = "+w); 89 90 */ 91 92 //測試,將序列化數據儲存在ByteArrayOutputStream中 93 94 //這個地方這樣寫,不便於後面操作 95 /* 96 ObjectOutputStream out2 = new ObjectOutputStream( 97 new ByteArrayOutputStream()); 98 */ 99 100 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 101 ObjectOutputStream out2 = new ObjectOutputStream(bs); 102 out2.writeObject("Worm Storage In Bytes...\n"); 103 out2.writeObject(w); 104 out2.flush(); 105 106 ObjectInputStream in2 = new ObjectInputStream( 107 new ByteArrayInputStream(bs.toByteArray())); 108 109 String s2 = (String)in2.readObject(); 110 Worm w3 = (Worm)in2.readObject(); 111 112 System.out.println(s2 + "w3 = "+w3); 113 114 115 } catch (FileNotFoundException e) { 116 e.printStackTrace(); 117 } catch (IOException e) { 118 e.printStackTrace(); 119 } catch (ClassNotFoundException e) { 120 e.printStackTrace(); 121 } 122 } 123 } 124 125 /* 126 Serializable序列化時,對象完全以它存儲的二進制為為基礎來構造,而不調用構造器 127 Externnlizable序列化時,所有普通的默認構造器都會被調用(包括字段定義時的初始 128 化),然後調用readExternal() 129 */ 130 class Blip implements Externalizable { 131 private int i; 132 private String s; 133 134 public Blip() { 135 System.out.println("Constructor: Blip()"); 136 } 137 138 public Blip(String s, int i) { 139 System.out.println("Constructor: Blip(String s, int i)"); 140 141 this.s=s; 142 this.i=i; 143 } 144 145 public String toString() { 146 return s + i; 147 } 148 149 @Override 150 public void writeExternal(ObjectOutput out) throws IOException { 151 System.out.println("Blip.writeExternal..."); 152 out.writeObject(s); 153 //這個地方寫了一個BUG,用Object會造成自動裝箱,導致readInt()讀不出來結果 154 //out.writeObject(i); 155 out.writeInt(i); 156 } 157 158 @Override 159 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 160 System.out.println("Blip.readExternal..."); 161 this.s = (String)in.readObject(); 162 this.i = in.readInt(); 163 } 164 165 public static void test() { 166 try{ 167 Blip b = new Blip("A String...", 47); 168 System.out.println(b); 169 170 //創建輸出流 171 ObjectOutputStream out = new ObjectOutputStream( 172 new FileOutputStream("Blip.out")); 173 out.writeObject(b); 174 out.close(); 175 176 //創建輸入流 177 ObjectInputStream in = new ObjectInputStream( 178 new FileInputStream("Blip.out")); 179 Blip b2 = (Blip)in.readObject(); 180 System.out.println(b2); 181 182 } catch (FileNotFoundException e) { 183 e.printStackTrace(); 184 } catch (IOException e) { 185 e.printStackTrace(); 186 } catch (ClassNotFoundException e) { 187 e.printStackTrace(); 188 } 189 190 191 } 192 } 193 194 /* 195 測試 Transient關鍵字 196 */ 197 class Login implements Serializable { 198 private Date date = new Date(); 199 private String username; 200 private transient String password; 201 202 public Login(String name, String password) { 203 this.username = name; 204 this.password=password; 205 } 206 207 public String toString() { 208 return "Login info:"+ 209 "\n username:"+username+ 210 "\n password:"+password+ 211 "\n date:"+date; 212 } 213 214 public static void test() { 215 try{ 216 Login l = new Login("zhangshan", "123456"); 217 System.out.println("Login l = "+l); 218 219 //創建輸出流 220 ObjectOutputStream out = new ObjectOutputStream( 221 new FileOutputStream("login.out")); 222 out.writeObject(l); 223 224 //創建輸入流 225 ObjectInputStream in = new ObjectInputStream( 226 new FileInputStream("login.out")); 227 Login m = (Login)in.readObject(); 228 System.out.println("Login m = "+m); 229 230 231 232 } catch (FileNotFoundException e) { 233 e.printStackTrace(); 234 } catch (IOException e) { 235 e.printStackTrace(); 236 } catch (ClassNotFoundException e) { 237 e.printStackTrace(); 238 } 239 240 241 } 242 } 243 244 /* 245 Externalizable的替代方案,非常混亂的一個方案,沒有研究的價值 246 */ 247 class SerialCtl implements Serializable { 248 private String username; 249 private transient String password; 250 251 public SerialCtl(String name, String password) { 252 this.username=name; 253 this.password=password; 254 } 255 256 public String toString() { 257 return username+"\n"+password; 258 } 259 260 private void writeObject(ObjectOutputStream outputStream) 261 throws IOException{ 262 outputStream.defaultWriteObject(); 263 outputStream.writeObject(password); 264 } 265 266 private void readObject(ObjectInputStream inputStream) 267 throws IOException,ClassNotFoundException{ 268 inputStream.defaultReadObject(); 269 password=(String)inputStream.readObject(); 270 } 271 272 public static void test() { 273 SerialCtl sc = new SerialCtl("Test1", "Test2"); 274 System.out.println("Before:\n"+sc); 275 276 try{ 277 //創建輸出流 278 ObjectOutputStream out = new ObjectOutputStream( 279 new FileOutputStream("SerialCtl.out")); 280 out.writeObject(sc); 281 282 //創建輸入流 283 ObjectInputStream in = new ObjectInputStream( 284 new FileInputStream("SerialCtl.out")); 285 SerialCtl sc2 = (SerialCtl)in.readObject(); 286 System.out.println("Now:\n"+sc2); 287 } catch (FileNotFoundException e) { 288 e.printStackTrace(); 289 } catch (IOException e) { 290 e.printStackTrace(); 291 } catch (ClassNotFoundException e) { 292 e.printStackTrace(); 293 } 294 } 295 }

Java編程思想:序列化基礎部分