浅谈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的属性不会丢失。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值