对象序列化 Serializable

对象序列化就是把一个对象变为二进制的数据流的一种方法,通过对象序列化,可以方便的实现对象的传输和储存


如果一个对象想要实现序列化,就要实现Serializable接口,改接口如下



改接口没有任何方法,所有此接口是一个标识,标识这个类具备了序列化的能力。


package com.example.jpa.test;

import java.io.Serializable;

/**
 * com.xinguangnet.tuchao.merchant.manage
 *
 * @Author : Wukn
 * @Date : 2018/2/5
 */
public class SerializableTest implements Serializable{
    
    /**
    *name
    */
    private String name;
    
    /**
     * in
     */
    private Integer id;

    public SerializableTest(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}



  /**
     * 对象输出流
     *
     *
     * 一个对象如果要进行输出,则必须使用ObjectOutputStream     * ObjectOutputStreamOutputStream的子类
     */
    public void test01() throws Exception{
        String s = "d:" + File.separator + "test.txt";
        File file = new File( s );
        //文件输出流
        OutputStream out = new FileOutputStream( file );
        //为对象输出流实例化
        ObjectOutputStream oo = new ObjectOutputStream( out );
        //保存对到文件
        oo.writeObject( new SerializableTest("D",1));
        oo.close();
    }
}

/**
 * 对象输入流
 *
 *
 * 一个对象如果要进行输入,则必须使用ObjectintputStream * ObjectinputStreaminputStream的子类
 */
public void test02() throws Exception{
    String str = "d:" + File.separator + "ttest.txt";
    File file = new File( str );
    //文件输入流
    InputStream in = new FileInputStream( file );
    //为对象输入流实例化
    ObjectInputStream oo = new ObjectInputStream( in );
    Object o =  oo.readObject();
    oo.close();
    //读取对象
    System.out.println(o);
    


Externalizable接口

被Serierializable接口申明的类的对象的内容都将被序列化,如果现在用户指定序列化的内容,则可以让这个类实现Externalizable接口

此接口的构造方法如下:


Externalizable接口是Serierializable的子类,此接口有两个方法



【注意】在使用Externalizable接口序列化时,该类一定要有一个无参的构造方法,不然会报错。

package com.example.jpa.test;

import java.io.*;

/**
 * com.xinguangnet.tuchao.merchant.manage
 *
 * @Author : Wukn
 * @Date : 2018/2/5
 */
public class SerializableTest implements Externalizable{

    /**
    *name
    */
    private String name;

    /**
     * in
     */
    private Integer id;

    public SerializableTest() {
    }

    public SerializableTest(String name, Integer id) {
        this.name = name;
        this.id = id;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    /** 重写此方法,根据需要保存需要的内容,序列化
     *
     * @param out
     * @throws IOException
     */
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject( this.name );
        out.writeObject( id );
    }

    /**
     *重写此方法,根据需要读取内容,反序列化
     * @param in
     * @throws IOException
     * @throws ClassNotFoundException
     */
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        this.name = (String) in.readObject();
        this.id = (Integer) in.readObject();
    }
}






package com.example.jpa.test;

import java.io.*;

/**
 * com.xinguangnet.tuchao.merchant.manage
 *
 * @Author : Wukn
 * @Date : 2018/2/5
 */
//对象序列化
public class tranientTest implements Serializable{

    /**
     *  //使用tranient表示这个属性不会被序列化
     */
    public transient String name;
    /**
     * id
     */
    private Integer id;

    public tranientTest(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public String toString() {
        return "tranientTest{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}

class Df{
    public static void main(String[] args) throws Exception {
      
    }

    /**
     * 系列化
     * @throws Exception
     */
    public void test01() throws Exception {
        String str = "d:" + File.separator + "test.txt";
        File file = new File( str );
        //文件输出流
        OutputStream ou = new FileOutputStream( file );
        //对象输出流
        ObjectOutputStream oo = new ObjectOutputStream( ou );
        oo.write(new tranientTest("we",12));
        oo.close();
    }

    /**
     * 反序列化
     * @throws Exception
     */
    public void test02() throws Exception {
        String str = "d:" + File.separator + "test.txt";
        File file = new File( str );
        //文件输出流
        InputStream ou = new FileInputStream( file );
        //对象输出流
        ObjectInputStream oo = new ObjectInputStream( ou );
        Object o = oo.readObject();
        oo.close();
        System.out.println(o);
    }

}











git@github.com:wukn2018/test.git 代码地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

托尼吴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值