Java Serializable

I. 简介

Serialisable 是一个接口;类在执行了Serialisable 接口后要进行IO操作时,系统会自动的把该类的对象封装.
通常在执行远程方法调用 (Remote Method Invocation, RMI)时,或者使用Java Bean 时需要封装一个类类对象,也就是需要改类执行Serialisable. 除了要类执行Serialisable 接口外,要序列化一个对象,要创建某些OutputStream 对象,然后封装在一个ObjectOutputStream 对象内,调用writeObject()便可将对象序列化.

II.Serializable实例

Serialisable 接口类似于一个标记,类在implements 后不需要执行任何方法.

public class Data implements Serializable{
    private static int number = 1;
    private int id = number++;
    private int n;
    public Data(int n) { this.n = n; }

    @Override
    public String toString() {
        return n + "  " + id;
    }

    public static void main(String [] args) 
            throws IOException, ClassNotFoundException {
        File file = new File("/Users/Desktop/object.txt");
        Data
                data = new Data(2),
                data2 = new Data(5);

        ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream(file));
        out.writeObject(data);
        out.writeObject(data2);

        ObjectInputStream in = new ObjectInputStream(
                new FileInputStream(file));
        data = (Data) in.readObject();
        System.out.println(data);
        data2 = (Data) in.readObject();
        System.out.println(data2);

        out.close();
        in.close();
    }
}

output://
2  1
5  2

Transient 关键字

如果在对象序列化后,我们不希望类中的某个变量被deserialisable,比如密码,那么在变量前加上serialisable 关键字即可.
例子仍未上述例子,但这是output 有所变化.

public class Data implements Serializable{
    private static int number = 1;
    private transient int id = number++;
    private int n;
    public Data(int n) { this.n = n; }

    @Override
    public String toString() {
        return n + "  " + id;
    }

    public static void main(String [] args) 
            throws IOException, ClassNotFoundException {
        File file = new File("/Users/Desktop/object.txt");
        Data
                data = new Data(2),
                data2 = new Data(5);

        ObjectOutputStream out = new ObjectOutputStream(
            new FileOutputStream(file));
        out.writeObject(data);
        out.writeObject(data2);

        ObjectInputStream in = new ObjectInputStream(
            new FileInputStream(file));
        data = (Data) in.readObject();
        System.out.println(data);
        data2 = (Data) in.readObject();
        System.out.println(data2);

        out.close();
        in.close();
    }
}

output://
2  0
5  0

因为id 前加上了transient 关键字,所以在deserialisable的过程中,id 值变为了0.

III. Externalizable 实例

Externalizable 是继承于Serializable 的接口,新增了两个方法writerExternal()readExternal().
在对象序列化时,就可以选择性地序列化变量了.

public class Data implements Externalizable{
    private static int number = 1;
    private int id = number++;
    private int n;
    public Data(int n) { this.n = n; }

    public Data() {}

    @Override
    public String toString() {
        return n + "  " + id;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.write(id);
        out.write(n);
    }

    @Override
    public void readExternal(ObjectInput in) 
            throws IOException, ClassNotFoundException {
        id = in.read();
        n = in.read();
    }

    public static void main(String [] args) 
            throws IOException, ClassNotFoundException {
        File file = new File("/Users/HereWegoR/Desktop/object.txt");
        Data
                data = new Data(2),
                data2 = new Data(5);

        ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream(file));
        out.writeObject(data);
        out.writeObject(data2);

        ObjectInputStream in = new ObjectInputStream(
                new FileInputStream(file));
        data = (Data) in.readObject();
        System.out.println(data);
        data2 = (Data) in.readObject();
        System.out.println(data2);

        out.close();
        in.close();
    }
}

output://
2  1
5  2

在使用Externalizable 时有两点需要注意:
1. 类需要有一个default constructor.
2. 需要序列化和反序列化的变量一定要在writerExternal()readExternal()方法中写入和读取.


另在使用Serializable 和Externalizable 介需要注意的是,接收序列化对象的机器上也要有相同的class 才能反序列化.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的Serializable是一个接口,用于实现对象的序列化和反序列化。当一个类实现了Serializable接口时,它的对象可以被转换为字节流,以便在网络上传输或者保存到文件中。同时,这些字节流也可以被反序列化为对象,以便在程序中重新使用。 以下是一个示例,演示了如何在Java中使用Serializable接口进行对象的序列化和反序列化: ```java import java.io.*; public class SerializationDemo { public static void main(String[] args) { // 创建一个Person对象 Person person = new Person(1, "John"); // 将Person对象序列化为字节流 try { FileOutputStream fileOut = new FileOutputStream("person.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(person); out.close(); fileOut.close(); System.out.println("Person对象已序列化并保存到person.ser文件中"); } catch (IOException e) { e.printStackTrace(); } // 从字节流中反序列化Person对象 try { FileInputStream fileIn = new FileInputStream("person.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); Person deserializedPerson = (Person) in.readObject(); in.close(); fileIn.close(); System.out.println("从person.ser文件中反序列化得到的Person对象为:" + deserializedPerson); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } ``` 请注意,要使一个类可序列化,需要满足以下条件: 1. 类必须实现Serializable接口。 2. 所有非静态和非瞬态的成员变量都必须是可序列化的。 3. 如果父类实现了Serializable接口,子类也会自动实现Serializable接口。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值