一、序列化
創(chuàng)新互聯(lián)建站從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元鹽湖做網(wǎng)站,已為上家服務(wù),為鹽湖各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
序列化定義:序列化是將對(duì)象狀態(tài)轉(zhuǎn)換為可保持或傳輸?shù)母袷降倪^(guò)程。與序列化相對(duì)的是反序列化,它將流轉(zhuǎn)換為對(duì)象。這兩個(gè)過(guò)程結(jié)合起來(lái),可以輕松地存儲(chǔ)和傳輸數(shù)據(jù)。
目的:
二、Java序列化
一個(gè)對(duì)象能夠序列化的前提是實(shí)現(xiàn)Serializable接口。Serializable接口沒(méi)有方法,更像是個(gè)標(biāo)記。有了這個(gè)標(biāo)記的Class就能被序列化機(jī)制處理。如下:
class myPoint implements Serializable{ }
JAVA反序列化不會(huì)調(diào)用任何構(gòu)造器
序列化的控制:Externalizable。讀寫都交給你
void writeExternal(ObjectOutput out) throws IOException; void readExternal(ObjectInput in) throws IOException,ClassNotFoundException; public class Point implements Externalizable { private int a; private int b; public Point(int a, int b) { this.a = a; this.b = b; } public Point() { } public String toString() { return a + " , " + b; } public void writeExternal(ObjectOutput out) throws IOException { out.write(a); out.write(b); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { a = in.read(); b = in.read(); } public static void main(String[] args) throws IOException, ClassNotFoundException { String file = "d://1.txt"; Point p = new Point(1, 2); System.out.println(p); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(p); FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); Point pp = (Point) ois.readObject(); System.out.println(pp); } }
三、序列化的問(wèn)題
在effective Java中列舉出了java序列化要注意的一些問(wèn)題:
1.謹(jǐn)慎地設(shè)計(jì)實(shí)現(xiàn)Serializable接口
2.保護(hù)性地編寫 readObject()方法,因?yàn)閞eadObject()是構(gòu)建實(shí)例的入口。
不保護(hù)可能出現(xiàn) 構(gòu)建了不滿足要求的 實(shí)例
3.考慮自定義的序列化形式
public class StringList implements Serializable { private transient int size = 0; private transient Entity head = null; public final void add(String str) { // ... } private static class Entity { String data; Entity next; Entity previous; } private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); s.write(size); for (Entity e = head; e != null; e = e.next) { s.writeObject(e.data); } } private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); int num = s.read(); for (int i = 0; i < num; i++) { this.add((String) s.readObject()); } } }
四、序列化代理模式
序列化機(jī)制提供的鉤子函數(shù)有:
writeReplace writeObject readObject readResolve
import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.Serializable; import java.util.Date; public final class Period implements Serializable { private static final long serialVersionUID = 100L; private final Date start; private final Date end; public Period(Date start, Date end) { this.start = new Date(start.getTime()); this.end = new Date(end.getTime()); if (this.start.compareTo(this.end) > 0) { throw new IllegalArgumentException(start + " after " + end); } } public Date start() { return new Date(start.getTime()); } public Date end() { return new Date(end.getTime()); } public String toString() { return start + " - " + end; } // 不給 private Object writeReplace() { return new SerializationProxy(this); } private void readObject(ObjectInputStream stream) throws InvalidObjectException { throw new InvalidObjectException("proxy request"); } private static class SerializationProxy implements Serializable { private final Date start; private final Date end; SerializationProxy(Period p) { this.start = p.start; this.end = p.end; } private Object readResolve() { return new Period(start, end); } private static final long serialVersionUID = 1000L; } }
五、序列化算法
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
當(dāng)前文章:Java序列化詳解及簡(jiǎn)單實(shí)現(xiàn)實(shí)例
網(wǎng)站鏈接:http://www.chinadenli.net/article20/jigico.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、App開發(fā)、商城網(wǎng)站、云服務(wù)器、網(wǎng)站維護(hù)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)