1. Overview
Java中的序列化就是將Java對象的狀態(tài)轉(zhuǎn)化為字節(jié)序列,以便存儲和傳輸?shù)臋C(jī)制,在未來的某個時間,可以通過字節(jié)序列重新構(gòu)造對象。把Java對象轉(zhuǎn) 換為字節(jié)序列的過程稱為對象的序列化。把字節(jié)序列恢復(fù)為Java對象的過程稱為對象的反序列化。這一切都?xì)w功于java.io包下的 ObjectInputStream和ObjectOutputStream這兩個類。
2. Serializable
要想實現(xiàn)序列化,類必須實現(xiàn)Serializable接口,這是一個標(biāo)記接口,沒有定義任何方法。如果一個類實現(xiàn)了Serializable接口,那么一旦這個類發(fā)布,“改變這個類的實現(xiàn)”的靈活性將大大降低。以下是一個序列化的小例子:
Java中的序列化就是將Java對象的狀態(tài)轉(zhuǎn)化為字節(jié)序列,以便存儲和傳輸?shù)臋C(jī)制,在未來的某個時間,可以通過字節(jié)序列重新構(gòu)造對象。把Java對象轉(zhuǎn) 換為字節(jié)序列的過程稱為對象的序列化。把字節(jié)序列恢復(fù)為Java對象的過程稱為對象的反序列化。這一切都?xì)w功于java.io包下的 ObjectInputStream和ObjectOutputStream這兩個類。
2. Serializable
要想實現(xiàn)序列化,類必須實現(xiàn)Serializable接口,這是一個標(biāo)記接口,沒有定義任何方法。如果一個類實現(xiàn)了Serializable接口,那么一旦這個類發(fā)布,“改變這個類的實現(xiàn)”的靈活性將大大降低。以下是一個序列化的小例子:
class Message implements Serializable{
private static final long serialVersionUID = 1L;
private String id;
private String content;
public Message(String id, String content){
this.id = id;
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String toString(){
return "id = " + id + " content = " + content;
}
}
public class Test{
public static void main(String[] args) {
serialize();
deserialize();
}
private static void serialize(){
Message message = new Message("1", "serializable test");
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Message"));
oos.writeObject(message);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("over");
}
private static void deserialize(){
try {商賬追收
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Message"));
Message message = (Message)ois.readObject();
System.out.println(message.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
private static final long serialVersionUID = 1L;
private String id;
private String content;
public Message(String id, String content){
this.id = id;
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String toString(){
return "id = " + id + " content = " + content;
}
}
public class Test{
public static void main(String[] args) {
serialize();
deserialize();
}
private static void serialize(){
Message message = new Message("1", "serializable test");
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Message"));
oos.writeObject(message);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("over");
}
private static void deserialize(){
try {商賬追收
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Message"));
Message message = (Message)ois.readObject();
System.out.println(message.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}