J2SE中的序列化詳解

          Java中處處體現(xiàn)著簡(jiǎn)單的程序設(shè)計(jì)風(fēng)格,序列化作為最常用的功能之一,在java中的設(shè)計(jì)尤為“簡(jiǎn)單”。在ObjectInputStream 和ObjectOutputStream的幫助下,我們可以輕松的實(shí)現(xiàn)序列化。

          只要我們的class 實(shí)現(xiàn)了java.io.Serializable接口,就可以利用ObjectOutputStream的writeObject()方法將一個(gè)對(duì)象序列化;利用ObjectInputStream的readObject()方法,可以返回讀出的object對(duì)象。Serializable接口不需要我們實(shí)現(xiàn)任何方法。

          以下是一個(gè)例子,它能給我們一個(gè)感性的初步認(rèn)識(shí):

          Serial實(shí)現(xiàn)了就java.io.Serializable接口,是需要序列化的類。我們首先構(gòu)造一個(gè)Serial的對(duì)象serial1然后將其保存(序列化)在一個(gè)文件中,而后再將其讀出(反序列化),并打印其內(nèi)容。
          package Stream;

          /**

          * @author favo yang

          */

          import java.io.*;

          public class Serial implements Serializable {

           int company_id;

           String company_addr;

           boolean company_flag;

           public Serial(){}//不同于C++,沒有也可以

           public Serial(int company_id,String company_addr,boolean company_flag) {

          this.company_id=company_id;

          this.company_addr=company_addr;

          this.company_flag=company_flag;

           }

           public static void main(String[] args) {

          Serial serial1 = new Serial(752,"dayer street #5 building 02-287",false);//構(gòu)造一個(gè)新的對(duì)象

          FileInputStream in=null;

          FileOutputStream out=null;

          ObjectInputStream oin=null;

          ObjectOutputStream oout=null;

          try {

           out = new FileOutputStream("5.txt");

           oout = new ObjectOutputStream(out);

           serial1.serialize(oout);//序列化

           oout.close();

           oout=null;

           in = new FileInputStream("5.txt");

           oin = new ObjectInputStream(in);

           Serial serial2 = Serial.deserialize(oin);//反序列化

           System.out.println(serial2);//打印結(jié)果

          } catch (Exception ex){

           ex.printStackTrace();

          } finally{

           try {

          if (in != null) {

           in.close();

          }

          if (oin != null) {

           oin.close();

          }

          if (out != null) {

           out.close();

          }

          if (oout != null) {

           oout.close();

          }

           } catch (IOException ex1) {

          ex1.printStackTrace();

           }

          }

           }

           /**

           * deserialize

           */

           public static Serial deserialize(ObjectInputStream oin) throws Exception{

          Serial s=(Serial)oin.readObject();

          return s;

           }

           public String toString() {

          return "DATA: "+company_id+" "+company_addr+" "+company_flag;

           }

           /**

          * serialize

           */

           public void serialize(ObjectOutputStream oout) throws Exception{

          oout.writeObject(this);

           }

          }

          運(yùn)行結(jié)果:DATA: 752 dayer street #5 building 02-287 false

          正確打印了結(jié)果。

          posted on 2006-11-22 20:00 Timothy 閱讀(168) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA大本營

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          語義Web : 網(wǎng)絡(luò)就是計(jì)算機(jī),軟件就是服務(wù)!

          常用鏈接

          留言簿(4)

          隨筆檔案(20)

          文章分類(25)

          文章檔案(25)

          新聞分類(4)

          新聞檔案(4)

          相冊(cè)

          收藏夾(27)

          個(gè)人主頁

          網(wǎng)絡(luò)博客

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 衡水市| 平定县| 安乡县| 浪卡子县| 敦煌市| 崇阳县| 育儿| 龙井市| 马鞍山市| 梓潼县| 苏州市| 东阳市| 怀化市| 正安县| 开江县| 精河县| 三台县| 锦屏县| 宁津县| 织金县| 樟树市| 九台市| 贡觉县| 修武县| 房产| 桂平市| 泉州市| 大化| 大余县| 东港市| 丹棱县| 靖安县| 洛隆县| 建阳市| 邵阳县| 格尔木市| 闽清县| 沈丘县| 淮滨县| 高要市| 金塔县|