手机版
你好,游客 登录 注册
背景:
阅读新闻

Java序列化机制的深入研究

[日期:2012-08-02] 来源:Linux社区  作者:zhaozheng7758 [字体:大 中 小]

7.3 超类具体化的具体化

当用具体化接口时,一个具体化对象必须运行writeExternal()方法存储对象的状态,用readExternal方法读取对象的状态。此例子验证了一个怎样存储和恢复它可具体化超类对象的状态。当一个可具体化对象的超类也具体化,子类要在它自己的writeExternal()和readExternal()方法中调用其超类的writeExternal()和readExternal()方法。

/*SaveSuper*/

package com.asc.alibaba.base;

 

import Java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

 

publicclass SaveSuper {

 

    publicstaticvoid main(String args[]) {

        Book1 bookorg = new Book1(100, "How to Serialize", true, "R.R", "Serialization", 97);

        Book1 booknew = null;

        try {

            FileOutputStream fo = new FileOutputStream("tmp");

            ObjectOutputStream so = new ObjectOutputStream(fo);

            so.writeObject(bookorg);

            so.flush();

        } catch (Exception e) {

            System.out.println(e);

            System.exit(1);

        }

        try {

            FileInputStream fi = new FileInputStream("tmp");

            ObjectInputStream si = new ObjectInputStream(fi);

            booknew = (Book1) si.readObject();

        } catch (Exception e) {

            System.out.println(e);

            System.exit(1);

        }

        System.out.println();

        System.out.println("Printing original book..");

        System.out.println(bookorg);

        System.out.println("Printing new book..");

        System.out.println(booknew);

        System.out.println("Both original and new should be the same");

        System.out.println();

    }

}

/*Book1*/

package com.asc.alibaba.base;

 

import java.io.Externalizable;

import java.io.IOException;

import java.io.ObjectInput;

import java.io.ObjectOutput;

 

class Book1 extends ReadingMaterial1 implements Externalizable {

 

    privateint     numpages;

    private String  name;

    privatebooleanishardcover;

 

    public Book1(){

        super();

    }

 

    public Book1(int pages, String n, boolean hardcover, String author, String subject, int yearwritten){

        super(author, subject, yearwritten);

        numpages = pages;

        name = n;

        ishardcover = hardcover;

    }

 

    publicvoid witeExternal(ObjectOutput out) throws IOException {

        super.writeExternal(out);

    }

 

    publicvoid readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

        super.readExternal(in);

        numpages = in.readInt();

        name = (String) in.readObject();

        ishardcover = in.readBoolean();

    }

 

    public String toString() {

        return ("Name:" + name + "\n" + "Author:" + super.getAuthor() + "\n" + "Pages:" + numpages + "\n" + "Subject:"

                + super.getSubject() + "\n" + "Year:" + super.getYearwritten() + "\n");

    }

}

 

/*ReadingMaterial1*/

package com.asc.alibaba.base;

 

import java.io.Externalizable;

import java.io.IOException;

import java.io.ObjectInput;

import java.io.ObjectOutput;

 

class ReadingMaterial1 implements Externalizable {

 

    private String author;

    private String subject;

    privateint    yearwritten;

 

    public ReadingMaterial1(){

    }

 

    public ReadingMaterial1(String auth, String sub, int year){

        author = auth;

        subject = sub;

        yearwritten = year;

    }

 

    public String getAuthor() {

        returnauthor;

    }

 

    public String getSubject() {

        returnsubject;

    }

 

    publicint getYearwritten() {

        returnyearwritten;

    }

 

    publicvoid writeExternal(ObjectOutput out) throws IOException {

        out.writeObject(author);

        out.writeObject(subject);

        out.writeInt(yearwritten);

    }

 

    publicvoid readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

        author = (String) in.readObject();

        subject = (String) in.readObject();

        yearwritten = in.readInt();

    }

}

结果如下:

Printing original book..

Name:How to Serialize

Author:R.R

Pages:100

Subject:Serialization

Year:97


Printing new book..

Name:null

Author:R.R

Pages:0

Subject:Serialization

Year:97

Both original and new should be the same

7.4 超类非具体化的具体化

当用具体化接口时,一个具体化对象必须运行writeExternal()方法存储对象的状态,用readExternal()方法读取对象的状态。此例子验证了一个对象怎样存储和恢复它非具体化超类的状态。当一个可具体化对象的超类没有具体化,子类必须用它自己的writeExternal()和readExternal()方法明确存储和恢复其超类的可具体化对象状态。

/*Nonexternsuper1*/

package com.asc.alibaba.base;

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

 

publicclass Nonexternsuper1 {

 

    publicstaticvoid main(String args[]) {

        Book2 bookorg = new Book2(100, "How to Serialize", true, "R.R", "Serialization", 97);

        Book2 booknew = null;

        try {

            FileOutputStream fo = new FileOutputStream("tmp");

            ObjectOutputStream so = new ObjectOutputStream(fo);

            so.writeObject(bookorg);

            so.flush();

        } catch (Exception e) {

            System.out.println(e);

            System.exit(1);

        }

        try {

            FileInputStream fi = new FileInputStream("tmp");

            ObjectInputStream si = new ObjectInputStream(fi);

            booknew = (Book2) si.readObject();

        } catch (Exception e) {

            System.out.println(e);

            System.exit(1);

        }

        System.out.println();

        System.out.println("Printing original book..");

        System.out.println(bookorg);

        System.out.println("Printing new book..");

        System.out.println(booknew);

        System.out.println("Both original and new should be the same");

        System.out.println();

    }

}

/*Book2*/

package com.asc.alibaba.base;

 

import java.io.Externalizable;

import java.io.IOException;

import java.io.ObjectInput;

import java.io.ObjectOutput;

 

class Book2 extends ReadingMaterial2 implements Externalizable {

 

    int     numpages;

    String  name;

    booleanishardcover;

 

    public Book2(){

        super();

    }

 

    Book2(int pages, String n, boolean hardcover, String author, String subject, int yearwritten){

        super(author, subject, yearwritten);

        numpages = pages;

        name = n;

        ishardcover = hardcover;

    }

 

    publicvoid writeExternal(ObjectOutput out) throws IOException {

        out.writeObject(author);

        out.writeObject(subject);

        out.writeInt(yearwritten);

        out.writeInt(numpages);

        out.writeObject(name);

        out.writeBoolean(ishardcover);

    }

 

    publicvoid readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

        author = (String) in.readObject();

        subject = (String) in.readObject();

        yearwritten = in.readInt();

        numpages = in.readInt();

        name = (String) in.readObject();

        ishardcover = in.readBoolean();

    }

 

    public String toString() {

        return ("Name:" + name + "\n" + "Author:" + author + "\n" + "Pages:" + numpages + "\n" + "Subject:" + subject

                + "\n" + "Year:" + yearwritten + "\n");

    }

}

/*ReadingMaterial2*/

package com.asc.alibaba.base;

 

publicclass ReadingMaterial2 {

 

    String author;

    String subject;

    int    yearwritten;

 

    public ReadingMaterial2(){

    }

 

    ReadingMaterial2(String auth, String sub, int year){

        author = auth;

        subject = sub;

        yearwritten = year;

    }

}

结果:

Printing original book..

Name:How to Serialize

Author:R.R

Pages:100

Subject:Serialization

Year:97

 

Printing new book..

Name:How to Serialize

Author:R.R

Pages:100

Subject:Serialization

Year:97

 

Both original and new should be the same

 

linux
相关资讯       Java序列化机制  Java序列化 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款