您好,登錄后才能下訂單哦!
一、序列化
序列化定義:序列化是將對象狀態轉換為可保持或傳輸的格式的過程。與序列化相對的是反序列化,它將流轉換為對象。這兩個過程結合起來,可以輕松地存儲和傳輸數據。
目的:
二、Java序列化
一個對象能夠序列化的前提是實現Serializable接口。Serializable接口沒有方法,更像是個標記。有了這個標記的Class就能被序列化機制處理。如下:
class myPoint implements Serializable{ }
JAVA反序列化不會調用任何構造器
序列化的控制: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); } }
三、序列化的問題
在effective Java中列舉出了java序列化要注意的一些問題:
1.謹慎地設計實現Serializable接口
2.保護性地編寫 readObject()方法,因為readObject()是構建實例的入口。
不保護可能出現 構建了不滿足要求的 實例
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()); } } }
四、序列化代理模式
序列化機制提供的鉤子函數有:
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; } }
五、序列化算法
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。