Java序列化与Serializable(一)OverView

本文介绍了序列化和反序列化的概念,即把数据结构或对象转换为便于存储、传输和重构的格式,反向操作则为反序列化。还阐述了Java中实现序列化的方法,即实现Serializable接口。此外,强调了手动申明serialVersionUID以保证兼容性,避免因类字段变动导致的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:https://en.wikipedia.org/wiki/Serialization

1.什么是序列化和反序列化

In computer science, in the context of data storage, serialization (or serialisation) is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later (possibly in a different computer environment)

翻译:序列化,就是把数据结构或者对象转换到一种便于存储,传输,以及重reconstructed 的格式的过程 

 

When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object

翻译:当resulting series of bits  被按照序列或的格式重新读取后,这个可以来创建一个最初的对象的语义上完全的clone

点评:突然想到有些科幻小说中,把人粒子化,然后光速传播到另一个地方,再重新reconstructed。感觉有点相似。

 

For many complex objects, such as those that make extensive use of references, this process is not straightforward. 

翻译:对一些复杂的对象,比如有大量应用,这个过程就不那么简单了。

 

Serialization of object-oriented objects does not include any of their associated methods with which they were previously linked.

翻译:面向对象的对象不包括之前和他们相关联的方法。

 

This process of serializing an object is also called marshalling an object.[2] The opposite operation, extracting a data structure from a series of bytes, is deserialization (also called unmarshalling).

翻译:序列化过程也叫marshalling an object 。反方向的操作,叫做反序列化(也叫作 unmarshalling

 

2.java中如何实现序列化

Java中实现序列化只要实现Serializable接口即可

举例:TestBean类(如果是在android中主要要申请读写权限)

public class TestBean implements Serializable {
    public String desc;
    public String property1;
    public String property2;

    @Override
    public String toString() {
        return "TestBean{" +
                "desc='" + desc + '\'' +
                ", property1='" + property1 + '\'' +
                ", property2='" + property2 + '\'' +
                '}';
    }
}

序列化以及反序列化的方法

 public void serialization(View view) {
        try {

            TestBean testBean = new TestBean();
            testBean.property1 = "属性1";
            testBean.property2 = "属性2";
            testBean.desc = "我是testbean";

            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("/sdcard/aaatest.txt"));
            objectOutputStream.writeObject(testBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void deserialization(View view) {
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream("/sdcard/aaatest.txt"));
            TestBean testBean = (TestBean) objectInputStream.readObject();
            Log.i(TAG,"deserialization"+testBean.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

查看aaatest.txt 文件,里面有一些乱码

 sr  calc.superdy.ttest.test.TestBean`仍旾?C L desct Ljava/lang/String;L     property1q ~ L     property2q ~ xpt 鎴戞槸testbeant 灞炴€?t 灞炴€?

经过上面的测试,序列化到文件中的对象通过反序列化过程可以正常读取

 

3.关于serialVersionUID

如果实现序列化了的类中有字段变动,切没有主动申明 serialVersionUID ,则系统会自动生成一个serialVersionUID

所以为了兼容,还是要自己手动申明 serialVersionUID

例如:写的时候是这样的结构

public class TestBean  implements Serializable {
    public String desc;
    public String property1;
    public String property2;

//    public static final int serialVersionUID = 1;
    @Override
    public String toString() {
        return "TestBean{" +
                "desc='" + desc + '\'' +
                ", property1='" + property1 + '\'' +
                ", property2='" + property2 + '\'' +
                '}';
    }
}

读的时候

public class TestBean  implements Serializable {
    public String desc;
    public String property1;
    public String property2;
    public String property3;
//    public static final int serialVersionUID = 1;
    @Override
    public String toString() {
        return "TestBean{" +
                "desc='" + desc + '\'' +
                ", property1='" + property1 + '\'' +
                ", property2='" + property2 + '\'' +
                '}';
    }
}

则会报如下错误:

/System.err: java.io.InvalidClassException: calc.superdy.ttest.test.TestBean; local class incompatible: stream classdesc serialVersionUID = 6974057760637855555, local class serialVersionUID = -1037588103870301871

 

 

内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值