【译】Java中的对象序列化
前言
好久沒(méi)翻譯simple java了,睡前來(lái)一篇。
譯文鏈接:
http://www.programcreek.com/2014/01/java-serialization/
什么是對(duì)象序列化
在Java中,對(duì)象序列化指的是將對(duì)象用字節(jié)序列的形式表示,這些字節(jié)序列包含了對(duì)象的數(shù)據(jù)和信息,一個(gè)序列化后的對(duì)象可以被寫(xiě)到數(shù)據(jù)庫(kù)或文件中,并且支持從數(shù)據(jù)庫(kù)或文件中反序列化,從而在內(nèi)存中重建對(duì)象;
為什么需要序列化
序列化經(jīng)常被用于對(duì)象的網(wǎng)絡(luò)傳輸或本地存儲(chǔ)。網(wǎng)絡(luò)基礎(chǔ)設(shè)施和硬盤(pán)只能識(shí)別位和字節(jié)信息,而不能識(shí)別Java對(duì)象。通過(guò)序列化能將Java對(duì)象轉(zhuǎn)成字節(jié)形式,從而在網(wǎng)絡(luò)上傳輸或存儲(chǔ)在硬盤(pán)。
那么為什么我們需要存儲(chǔ)或傳輸對(duì)象呢?根據(jù)我的編程經(jīng)驗(yàn),有如下原因需要將對(duì)象序列化(以下原因,我表示沒(méi)使用過(guò)。。。):
- 一個(gè)對(duì)象的創(chuàng)建依賴很多上下文環(huán)境,一旦被創(chuàng)建,它的方法和屬性會(huì)被很多其它組件所使用;
- 一個(gè)包含了很多屬性的對(duì)象創(chuàng)建后,我們并不清楚如何使用這些屬性,所以將它們存儲(chǔ)到數(shù)據(jù)庫(kù)用于后續(xù)的數(shù)據(jù)分析;
順便也說(shuō)下,根據(jù)我(真正的我)的編程經(jīng)驗(yàn),序列化使用情況如下:
- 網(wǎng)絡(luò)上的對(duì)象傳輸
- 使用一些緩存框架的時(shí)候,比如ehcache,將對(duì)象緩存到硬盤(pán)的時(shí)候,需要序列化,還有hibernate也會(huì)用到;
- RMI(遠(yuǎn)程方法調(diào)用)
Java序列化例子
以下代碼展示了如何讓一個(gè)類可序列化,對(duì)象的序列化以及反序列化;
對(duì)象:
package serialization;import java.io.Serializable;public class Dog implements Serializable {private static final long serialVersionUID = -5742822984616863149L;private String name;private String color;private transient int weight;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public int getWeight() {return weight;}public void setWeight(int weight) {this.weight = weight;}public void introduce() {System.out.println("I have a " + color + " " + name + ".");} }main方法
package serialization;import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream;public class SerializeDemo {public static void main(String[] args) {// create an objectDog e = new Dog();e.setName("bulldog");e.setColor("white");e.setWeight(5);// serializetry {FileOutputStream fileOut = new FileOutputStream("./dog.ser");ObjectOutputStream out = new ObjectOutputStream(fileOut);out.writeObject(e);out.close();fileOut.close();System.out.printf("Serialized dog is saved in ./dog.ser");} catch (IOException i) {i.printStackTrace();}e = null;// Deserializetry {FileInputStream fileIn = new FileInputStream("./dog.ser");ObjectInputStream in = new ObjectInputStream(fileIn);e = (Dog) in.readObject();in.close();fileIn.close();} catch (IOException i) {i.printStackTrace();return;} catch (ClassNotFoundException c) {System.out.println("Dog class not found");c.printStackTrace();return;}System.out.println("\nDeserialized Dog ...");System.out.println("Name: " + e.getName());System.out.println("Color: " + e.getColor());System.out.println("Weight: " + e.getWeight());e.introduce();} }結(jié)果打印:
Serialized dog is saved in ./dog.ser
Deserialized Dog ...
Name: bulldog
Color: white
Weight: 0
I have a white bulldog.
本文轉(zhuǎn)自風(fēng)一樣的碼農(nóng)博客園博客,原文鏈接:http://www.cnblogs.com/chenpi/p/5582492.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者總結(jié)
以上是生活随笔為你收集整理的【译】Java中的对象序列化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 订单自动生成器的算法研究与实现
- 下一篇: Java 添加播放MIDI音乐