浅谈JavaBean序列化(一)

昨天晚上在网站Top看到一个朋友提到序列化,他做了很详细的剖析,我觉得很好。又因为以前大学毕业论文弄的消息中间件(JMS),刚好用到了序列化,所以把Java的序列化再给整理一下。

 

   定义一个JavaBean 并实现Serializable接口

 

package org.fuzzy.bottle.bean;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: caiwm
 * Date: 12-7-3
 * Time: 下午10:13
 * To change this template use File | Settings | File Templates.
 */
public class RemoteBean implements Serializable {
    private String locateAddress;
    private String remoteAddress;
    private String locateImsi;
    private String locateBigValue;

    public String getLocateAddress() {
        return locateAddress;
    }

    public void setLocateAddress(String locateAddress) {
        this.locateAddress = locateAddress;
    }

    public String getRemoteAddress() {
        return remoteAddress;
    }

    public void setRemoteAddress(String remoteAddress) {
        this.remoteAddress = remoteAddress;
    }

    public String getLocateImsi() {
        return locateImsi;
    }

    public void setLocateImsi(String locateImsi) {
        this.locateImsi = locateImsi;
    }

    public String getLocateBigValue() {
        return locateBigValue;
    }

    public void setLocateBigValue(String locateBigValue) {
        this.locateBigValue = locateBigValue;
    }
}

 写个测试方法:

 

    public static void main(String[] args) throws IOException {
        RemoteBean remoteBean = new RemoteBean();
        remoteBean.setLocateAddress("Http://www.iteye.com/holly-shite/write-page.jsp");
        remoteBean.setLocateImsi("110M55876384991S");
        remoteBean.setRemoteAddress("http://localhost:1111/33-july-judy.php");

        FileOutputStream fos = new FileOutputStream("temp.out");
        ObjectOutputStream oos  = new ObjectOutputStream(fos);
        oos.writeObject(remoteBean);
        oos.flush();
        oos.close();
        fos.flush();
        fos.close();
    }

 序列化,把流输出到文件中,可以看到持久化的数据如下:


浅谈JavaBean序列化(一)

 

-----------------------

序列化传输,对于分布式系统尤为重要。不仅效率很好,而且JavaBean的属性不会丢失。

你可能感兴趣的:(java,Serializable,javabean)