Java 日看一类(54)之IO包中的Serializable接口

【Java基础】之IO篇——Serializable Java基础之IO篇——Serializable 阅读详情

该接口无引入包

无继承类



该接口的类头注释如下:

/**
 * Serializability of a class is enabled by the class implementing the
 * java.io.Serializable interface. Classes that do not implement this
 * interface will not have any of their state serialized or
 * deserialized.  All subtypes of a serializable class are themselves
 * serializable.  The serialization interface has no methods or fields
 * and serves only to identify the semantics of being serializable. <p>
 *
 * To allow subtypes of non-serializable classes to be serialized, the
 * subtype may assume responsibility for saving and restoring the
 * state of the supertype's public, protected, and (if accessible)
 * package fields.  The subtype may assume this responsibility only if
 * the class it extends has an accessible no-arg constructor to
 * initialize the class's state.  It is an error to declare a class
 * Serializable if this is not the case.  The error will be detected at
 * runtime. <p>
 *
 * During deserialization, the fields of non-serializable classes will
 * be initialized using the public or protected no-arg constructor of
 * the class.  A no-arg constructor must be accessible to the subclass
 * that is serializable.  The fields of serializable subclasses will
 * be restored from the stream. <p>
 *
 * When traversing a graph, an object may be encountered that does not
 * support the Serializable interface. In this case the
 * NotSerializableException will be thrown and will identify the class
 * of the non-serializable object. <p>
 *
 * Classes that require special handling during the serialization and
 * deserialization process must implement special methods with these exact
 * signatures:
 *
 * <PRE>
 * private void writeObject(java.io.ObjectOutputStream out)
 *     throws IOException
 * private void readObject(java.io.ObjectInputStream in)
 *     throws IOException, ClassNotFoundException;
 * private void readObjectNoData()
 *     throws ObjectStreamException;
 * </PRE>
 *
 * <p>The writeObject method is responsible for writing the state of the
 * object for its particular class so that the corresponding
 * readObject method can restore it.  The default mechanism for saving
 * the Object's fields can be invoked by calling
 * out.defaultWriteObject. The method does not need to concern
 * itself with the state belonging to its superclasses or subclasses.
 * State is saved by writing the individual fields to the
 * ObjectOutputStream using the writeObject method or by using the
 * methods for primitive data types supported by DataOutput.
 *
 * <p>The readObject method is responsible for reading from the stream and
 * restoring the classes fields. It may call in.defaultReadObject to invoke
 * the default mechanism for restoring the object's non-static and
 * non-transient fields.  The defaultReadObject method uses information in
 * the stream to assign the fields of the object saved in the stream with the
 * correspondingly named fields in the current object.  This handles the case
 * when the class has evolved to add new fields. The method does not need to
 * concern itself with the state belonging to its superclasses or subclasses.
 * State is saved by writing the individual fields to the
 * ObjectOutputStream using the writeObject method or by using the
 * methods for primitive data types supported by DataOutput.
 *
 * <p>The readObjectNoData method is responsible for initializing the state of
 * the object for its particular class in the event that the serialization
 * stream does not list the given class as a superclass of the object being
 * deserialized.  This may occur in cases where the receiving party uses a
 * different version of the deserialized instance's class than the sending
 * party, and the receiver's version extends classes that are not extended by
 * the sender's version.  This may also occur if the serialization stream has
 * been tampered; hence, readObjectNoData is useful for initializing
 * deserialized objects properly despite a "hostile" or incomplete source
 * stream.
 *
 * <p>Serializable classes that need to designate an alternative object to be
 * used when writing an object to the stream should implement this
 * special method with the exact signature:
 *
 * <PRE>
 * ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
 * </PRE><p>
 *
 * This writeReplace method is invoked by serialization if the method
 * exists and it would be accessible from a method defined within the
 * class of the object being serialized. Thus, the method can have private,
 * protected and package-private access. Subclass access to this method
 * follows java accessibility rules. <p>
 *
 * Classes that need to designate a replacement when an instance of it
 * is read from the stream should implement this special method with the
 * exact signature.
 *
 * <PRE>
 * ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
 * </PRE><p>
 *
 * This readResolve method follows the same invocation rules and
 * accessibility rules as writeReplace.<p>
 *
 * The serialization runtime associates with each serializable class a version
 * number, called a serialVersionUID, which is used during deserialization to
 * verify that the sender and receiver of a serialized object have loaded
 * classes for that object that are compatible with respect to serialization.
 * If the receiver has loaded a class for the object that has a different
 * serialVersionUID than that of the corresponding sender's class, then
 * deserialization will result in an {@link InvalidClassException}.  A
 * serializable class can declare its own serialVersionUID explicitly by
 * declaring a field named <code>"serialVersionUID"</code> that must be static,
 * final, and of type <code>long</code>:
 *
 * <PRE>
 * ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
 * </PRE>
 *
 * If a serializable class does not explicitly declare a serialVersionUID, then
 * the serialization runtime will calculate a default serialVersionUID value
 * for that class based on various aspects of the class, as described in the
 * Java(TM) Object Serialization Specification.  However, it is <em>strongly
 * recommended</em> that all serializable classes explicitly declare
 * serialVersionUID values, since the default serialVersionUID computation is
 * highly sensitive to class details that may vary depending on compiler
 * implementations, and can thus result in unexpected
 * <code>InvalidClassException</code>s during deserialization.  Therefore, to
 * guarantee a consistent serialVersionUID value across different java compiler
 * implementations, a serializable class must declare an explicit
 * serialVersionUID value.  It is also strongly advised that explicit
 * serialVersionUID declarations use the <code>private</code> modifier where
 * possible, since such declarations apply only to the immediately declaring
 * class--serialVersionUID fields are not useful as inherited members. Array
 * classes cannot declare an explicit serialVersionUID, so they always have
 * the default computed value, but the requirement for matching
 * serialVersionUID values is waived for array classes.
 *
 * @author  unascribed
 * @see java.io.ObjectOutputStream
 * @see java.io.ObjectInputStream
 * @see java.io.ObjectOutput
 * @see java.io.ObjectInput
 * @see java.io.Externalizable
 * @since   JDK1.1
 */

该类的类头注释如下:

类通过实现该接口来拥有序列化功能

没有实现该类的无法进行序列化或者反序列化

可序列化类的子类也可序列化

该结构未定义任何的字段以及方法,只是个标识,标志该类可以被序列化

如果允许不可序列化类的子类进行序列化,该子类必须负责父类字段值的储存和恢复

仅在子类型扩展的类有一个可访问的无参数构造方法来初始化该类的状态时,才可以假定子类型有此职责

当不存在无参数构造方法时,声明序列化是错误的,会在运行时报错


在反序列化期间,不可序列化的类的字段值需要通过子类的无参数可访问的(public,protected)构造方法来恢复(子类是可序列化的)。可序列化类的子类的字段会从流中恢复


遍历图形时,可能会遇到不支持Serializable的对象,在这种情况下抛出NotSerializionException异常


在序列化和反序列化过程中需要特殊处理的类必须使用下列准确标识来实现特殊方法:

 private void writeObject(java.io.ObjectOutputStream out)
     throws IOException
 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;
 private void readObjectNoData() 
     throws ObjectStreamException;

writeObject方法负责写出特指的对象状态,相应的、readObject方法用于回复特定的对象状态

通过调用 out.defaultWriteObject 可以调用保存 Object 的字段的默认机制。该方法本身不需要涉及属于其超类或子类的状态。通过使用 writeObject 方法或使用 DataOutput 支持的用于基本数据类型的方法将各个字段写入 ObjectOutputStream,状态可以被保存。

readObject 方法负责从流中读取并恢复类字段。它可以调用 in.defaultReadObject 来调用默认机制,以恢复对象的非静态和非瞬态字段。defaultReadObject 方法使用流中的信息来分配流中通过当前对象中相应指定字段保存的对象的字段。这用于处理类演化后需要添加新字段的情形。该方法本身不需要涉及属于其超类或子类的状态。通过使用 writeObject 方法或使用 DataOutput 支持的用于基本数据类型的方法将各个字段写入 ObjectOutputStream,状态可以被保存。

在序列化流不列出给定类作为将被反序列化对象的超类的情况下,readObjectNoData 方法负责初始化特定类的对象状态。这在接收方使用的反序列化实例类的版本不同于发送方,并且接收者版本扩展的类不是发送者版本扩展的类时发生。在序列化流已经被篡改时也将发生;因此,不管源流是“敌意的”还是不完整的,readObjectNoData 方法都可以用来正确地初始化反序列化的对象。

将对象写入流时需要指定要使用的替代对象的可序列化类,应使用准确的签名来实现此特殊方法:

 ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
 

此 writeReplace 方法将由序列化调用,前提是如果此方法存在,而且它可以通过被序列化对象的类中定义的一个方法访问。因此,该方法可以拥有私有 (private)、受保护的 (protected) 和包私有 (package-private) 访问。子类对此方法的访问遵循 java 访问规则。

在从流中读取类的一个实例时需要指定替代的类应使用的准确签名来实现此特殊方法。

 ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
 

此 readResolve 方法遵循与 writeReplace 相同的调用规则和访问规则。

序列化运行时使用一个称为 serialVersionUID 的版本号与每个可序列化类相关联,该序列号在反序列化过程中用于验证序列化对象的发送者和接收者是否为该对象加载了与序列化兼容的类。如果接收者加载的该对象的类的 serialVersionUID 与对应的发送者的类的版本号不同,则反序列化将会导致 InvalidClassException。可序列化类可以通过声明名为 "serialVersionUID" 的字段(该字段必须是静态 (static)、最终 (final) 的 long 型字段)显式声明其自己的 serialVersionUID:

 ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
 
如果可序列化类未显式声明 serialVersionUID,则序列化运行时将基于该类的各个方面计算该类的默认 serialVersionUID 值,如“Java(TM) 对象序列化规范”中所述。不过, 强烈建议  所有可序列化类都显式声明 serialVersionUID 值,原因是计算默认的 serialVersionUID 对类的详细信息具有较高的敏感性,根据编译器实现的不同可能千差万别,这样在反序列化过程中可能会导致意外的  InvalidClassException 。因此,为保证 serialVersionUID 值跨不同 java 编译器实现的一致性,序列化类必须声明一个明确的 serialVersionUID 值。还强烈建议使用  private  修饰符显示声明 serialVersionUID(如果可能),原因是这种声明仅应用于直接声明类 -- serialVersionUID 字段作为继承成员没有用处。数组类不能声明一个明确的 serialVersionUID,因此它们总是具有默认的计算值,但是数组类没有匹配 serialVersionUID 值的要求。



大部分内容来源于api,该类主要为序列化服务,所以需要使用序列化功能的请仔细学习阅读该注释

Spark 报错 no Java class corresponding to Product with Serializable found spark 阅读详情

相关推荐

54、Java IO 相关类详解

本文详细介绍了 Java IO 包中的多个核心类和接口,包括与对象序列化相关的 ObjectStreamConstants、ObjectStreamException 和 ObjectStreamField,以及处理输入输出的 OutputStream、OutputStreamWriter、PipedInputStream、PipedOutputStream、PipedWriter、PrintStream 和 PrintWriter。文章通过类定义、功能描述和应用场景分析,帮助开发者全面了解 Java IO

vscode6remote的博客 37

java.lang.io包 Serializable

序列化是什么:序列化就是将一个对象的状态(各个属性量)保存起来,然后在适当的时候再获得。 序列化分为两大部分:序列化和反序列化。序列化是这个过程的第一部分,将数据分解成字节流,以便存储在文件中或在网络上传输。反序列化就是打开字节流并重 构对象。对象序列化不仅要将基本数据类型转换成字节表示,有时还要恢复数据。恢复数据要求有恢复数据的对象实例序列化的什么特点:如果某个类能够被序列 化,其子类...

e家 330

MS建模mmt

一、流程:1.定义好每个原子力场类型:Edit Sets(分组) 2.加H(一般是仅与Al-O八面体中的Al相连的O原子)、给H分组 3.“make P1”、校核化学式(正确与否) 4.扩成超晶胞(Na-mmt:4*1*1,Ca-mmt:4*2*1,illite:2*1*1,由化学式而定) 5.置换阳离子: mmt:模型中每 32 个 Si 4+有 1 个被 Al 3+ 取...

weixin_30721899的博客 2174

JAVA基础 - Serializable的作用与用法

Serializable是java.io包中定义的,用于将JAVA对象进行序列化和反序列化。序列化,将一个JAVA对象以二进制流的方式持久化到数据库、文件中,或者用于大对象暂存、网络传输、使用序列化实现深度克隆;反序列化,将之前持久化在数据库或文件中的二进制数据,以流的方式读取出来重新构造成一个和之前内容相同的JAVA对象。Serializable序列化接口没有任何方法或者字段,只是用于标识可序列化的语义。...

记录并分享 3236

Qwen-Image LoRA训练实战:从数据准备到异常修复的全流程解析

本文详细解析了Qwen-Image LoRA训练的全流程,从数据准备到参数优化再到异常修复。通过实战案例展示了如何利用LoRA技术微调Qwen-Image模型,提升中文场景下的图像生成质量,特别针对手脚生成异常等问题提供了有效解决方案。文章还分享了中文提示词优化技巧和高级训练方法,帮助开发者高效训练出高质量的LoRA模型。

lemon的博客 969

LPDDR4协议规范之 (四)命令和时序

LPDDR4协议规范之 (四)命令和时序命令真值表激活操作读写访问操作读前沿和后沿突发读取操作流程读操作时序写前沿和后沿突发写操作写入均衡(待更新) 命令真值表 激活操作 ACTIVATE命令由两个连续命令组成,Activate-1命令和Activate-2。通过在(Activate-1)时钟的第一个上升沿保持CS HIGH,CA0 HIGH和CA1 LOW来发出Activate-1命令,通过...

YJFeiii的博客 2万+

JDK1.8源码笔记(4) Serializable

关于Serializable Serializability of a class is enabled by the class implementing the java.io.Serializable interface. 一个类序列化的能力通过实现java.io.Serializable接口可以开启。 Classes that do not implement this interf...

lalala_HFUT的博客 585

头歌OpenGauss数据库 - 单表查询 答案

本关任务:用SELECT语句检索数据表中指定字段的数据;用SELECT语句检索数据表中所有字段的数据。为了完成本关任务,你需要掌握: 1.如何获取数据表中指定字段的数据;2.如何获取数据表中的所有数据。

d214493517的博客 5722

serializable 序列化id_【54期】Java序列化三连问,是什么?为什么需要?如何实现?...

来源:https://blog.youkuaiyun.com/qq_27093465/遇到这个 Java Serializable 序列化这个接口,我们可能会有如下的问题什么叫序列化和反序列化作用,为啥要实现这个 Serializable 接口,也就是为啥要序列化serialVersionUID 这个的值到底是在怎么设置的,有什么用。有的是1L,有的是一长串数字,迷惑ing。我刚刚见到这个关键字 Serial...

weixin_39805355的博客 149

keil自动分配SDRAM空间设置使用

1.修改.sct文件。

qizhi321123的博客 1404

Ubuntu使用gstreamer开发USB相机应用(一)

Ubuntu使用gstreamer开发USB相机应用(一) 包括:gstreamer安装,测试。使用v4l2测试USB相机。使用gstreamer采集相机

Alon1787的博客 2739

Java 标记接口

  没有声明或定义方法的接口称为标记接口(Mark Interface)。某个类实现该接口时不需要重写方法,表明具有接口标记的功能。Java中常用的3个标记接口如下:   1Serializable   java.io.Serializable接口用于标记类是否支持序列化。序列化指将Java对象转换成二进制字节码。如果一个类没有实现该接口,该类实例执行序列化操作,那么JVM会抛出N...

aotangai1607的博客 204

Unity之OpenXR+XR Interaction Toolkit实现 Gaze眼部追踪

在虚拟现实(VR)和增强现实(AR)应用中,眼动追踪是一项强大而受欢迎的技术。它可以让开发者更好地理解用户的注意力和行为,并创造出更加沉浸和智能的体验。在本文中,我们将探讨如何使用 Unity OpenXR 实现Gaze眼部追踪功能。

TxNet 1752

AI Agent选型对比:商业平台、自建与Dify的决策之道

AI Agent正成为企业智能化转型的核心载体,而选型决策往往决定落地成败。商业云平台、自研编排框架与开源平台Dify分别代表托管服务、研发能力与基础设施中间件三种路线,其技术原理与成本结构差异显著。从工程实践看,多租户隔离、工作流可视化编排、RAG知识库接入及可观测性,是评估平台能否扛住生产级业务的关键指标。面对数据主权与安全合规要求,私有化部署未必是唯一解,需综合考量团队能力与长期演进成本。不同规模企业可依据六步选型法,在快速验证、自主可控与合规约束间找到平衡,实现Agent从Demo到业务价值的平稳落

weixin_34166847的博客 305

面经-2023-联发科MTK-数字芯片设计

面经-2023-联发科MTK-数字芯片设计

w0shishabi的博客 2945

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

序列化 Serializable

1.Serializable 类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化,并抛出异常; 异常代码: 1 java.io.NotSerializableException: cn.a2.serializable.Student 2 at java.io.ObjectOutputStream....

weixin_34342578的博客 91

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

java io系列06之 序列化总结(Serializable 和 Externalizable)

本章,我们对序列化进行深入的学习和探讨。学习内容,包括序列化的作用、用途、用法,以及对实现序列化的2种方式Serializable和Externalizable的深入研究。 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_06.html 1. 序列化是的作用和用途 序列化,就是为了保存对象的状态;而与之对应的反序列化,则可以把保存

XINJing的专栏 427

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

【54期】Java序列化三连问,是什么?为什么需要?如何实现?

点击上方蓝色“java大数据修炼之道”,选择“设为星标”每晚九点: 技术干货 ???? 必定奉上遇到这个 Java Serializable 序列化这个接口,我们可能会有如下的问题什么叫序列化和反序列化作用,为啥要实现这个 Serializable 接口,也就是为啥要序列化serialVersionUID 这个的值到底是在怎么设置的,有什么用。有的是1L,有的是一长串数字,迷...

hello world! 222

Serializable 序列化和反序列化

遇到这个 Java Serializable 序列化这个接口,我们可能会有如下的问题:a)什么叫序列化和反序列化b)作用。为啥要实现这个 Serializable 接口,也就是为啥要序列化c)serialVersionUID 这个的值到底是在怎么设置的,有什么用。有的是1L,有的是一长串数字,迷惑ing。 我刚刚见到这个关键字 Serializable 的时候,就有如上的这么些...

diejikuai0181的博客 114

54面试常问:Java序列化三连问,是什么?为什么需要?如何实现?

遇到这个 Java Serializable 序列化这个接口,我们可能会有如下的问题 什么叫序列化和反序列化 作用,为啥要实现这个 Serializable 接口,也就是为啥要序列化 serialVersionUID 这个的值到底是在怎么设置的,有什么用。有的是1L,有的是一长串数字,迷惑ing。 我刚刚见到这个关键字 Serializable 的时候,就有如上的这么些问题。 在处理这个问题之前,你要先知道一个问题,这个比较重要。 这个Serializable接...

AI乔治 270

Java中IO体系

我认为可以用“字节流操作类”和“字符流从操作类”组成Java IO流体系 1,基于字节流: InputStream:FileInputStream、ByteArrayInputStream、StringBufferInputStream、SequenceInputStream、PipledInputStream、ObjectInputStream、FilterInputStream(Buffe...

Joker 138

IO包中的其他类

查看各对象API文档 打印流 PrintWriter PrintStream 序列流:对多个流进行排列合并 SequenceInputStream 1 public static void main(String[] args) throws IOException { 2 3 4 /* 5 * 需求:将...

weixin_30869099的博客 60

实现serializable序列化接口

1、什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字节描述的过程;反序列化deserialization是一种将这些字节重建成一个对象的过程。 2、什么情况下需要序列化  a)当你想把的内存中的对象保存到一个文件中或者数据库中时候; b)当你想用套接字在网络上传送对象的时候; c)当你想通过RMI传输对象的时候; 3、如何

c_zyer的博客 571

Java 对象序列化和反序列化 (实现 Serializable 接口)

序列化和反序列化的概念   把对象转换为字节序列的过程称为对象的序列化。  把字节序列恢复为对象的过程称为对象的反序列化。   对象的序列化主要有两种用途:  1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;  2) 在网络上传送对象的字节序列。 JDK类库中的序列化API   java.io.ObjectOutputStream代表对象输出流,它的...

dei5960的博客 153
上一篇: Java 日看一类(53)之IO包中的SerialCallbackContext类
下一篇: Java 日看一类(55)之IO包中的StringReader类
wyf95278
博客等级 码龄10年 38粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值