對象序列化
對象保存到磁盤用對象輸出流。
writeObject? 放入某一對象內(nèi)。
readObject? 讀取某一信息返回類型是Object 。
繼承Serializable接口。
InetAddress.getLocalHost()獲取IP地址。
定制對象序列化
如:private void writeObject(ObjectOutputStream out) throws IOException {
??????????????????String pwd = changed(userpwd);
??????????????????out.writeObject(name);
??????????????????out.writeObject(age);
??????????????????out.writeObject(addr);
??????????????????out.writeObject(pwd);
?????????}
?????????private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
??????????????????this.name = (String)in.readObject();
??????????????????this.age = (Integer)in.readObject();
??????????????????this.addr = (String)in.readObject();
??????????????????this.userpwd = (String)in.readObject();
?????????}
?????????//密碼反轉(zhuǎn)的功能
?????????private String changed(String in) {
??????????????????StringBuffer sb = new StringBuffer(in);
??????????????????sb.reverse();
??????????????????return sb.toString();
?????????}
?????????public String toString() {
??????????????????return "Name:"+name+"\nAge:"+age+"\nAddr:"+addr+"\nPwd:"+userpwd+"\n\n";
?????????}
transient關(guān)鍵字限制屬性寫入到文件或網(wǎng)絡(luò)中,避免NotSerializableException異常。
transient關(guān)鍵字用在定制對象序列化中是不起作用的。
System.out.println(obj);??? 會自動調(diào)用toString方法。
static屬性,方法不被序列化;對屬性和類進(jìn)行序列化。
?