Java 对象序列化与反序列化
程序员文章站
2023-11-12 09:27:34
对象序列化 对象序列化 对象序列化定义 所谓的对象序列化就是将 保存在内存中的对象数据转换为二进制数据流进行传输的操作 ;但不是所有对象都可以进行序列化,要被序列化的的对象那么其所在的类一定要实现 java.io.Serializable 接口,该接口并没有认识的操作方法,因为该接口是一个 标识接口 ......
对象序列化
对象序列化
对象序列化定义
所谓的对象序列化就是将 保存在内存中的对象数据转换为二进制数据流进行传输的操作 ;但不是所有对象都可以进行序列化,要被序列化的的对象那么其所在的类一定要实现 java.io.serializable 接口,该接口并没有认识的操作方法,因为该接口是一个 标识接口 。
可以被序列化的类
import java.io.serializable; @suppresswarnings("serial") class book implements serializable { private string title ; private double price ; public book(string title , double price) { this.price = price ; this.title = title ; } public string tostring() { return "this.title + "\t" + this.price"; } }
如上,就实现了一个可以被序列化的类,(使用了压制警告)。
如此,book类就可以实现二进制的传输了!
实现序列化和反序列化
序列化类:
- java.io.objectoutputstream
- 将对象转为指定格式的二进制数据
- 构造方法:
public objectoutputstream(outputstream out)
- 输出对象:
public final void writeobject(object obj)
反序列化类:
- java.io.objectinputstream
- 将已经序列化的对象转换回原本的对象内容
- 构造方法:
public objectinputstream(inputstream in)
- 读取对象:
public final object readobject()
实现序列化对象操作
@suppresswarnings("serial") class book implements serializable { private string title ; private double price ; public book(string title , double price) { this.price = price ; this.title = title ; } public string tostring() { return this.title + "\t" + this.price ; } } public class testdemo { public static void main(string [] args) throws exception { ser(); } public static void ser() throws exception { objectoutputstream oos = new objectoutputstream(new fileoutputstream(new file("f:" + file.separator + "demo" + file.separator + "byte.txt"))); oos.writeobject(new book("java开发",110.1)); oos.close(); } }
实现反序列化类
public static void dser() throws exception { objectinputstream ois = new objectinputstream(new fileinputstream (new file("f:" + file.separator + "demo" + file.separator + "byte.txt"))); object obj = ois.readobject();// 按照object读取 book book = (book) obj;// 向下转型 system.out.println(book); ois.close(); }
序列化与反序列化完整实现
package helloworld; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.serializable; @suppresswarnings("serial") class book implements serializable { private string title ; private double price ; public book(string title , double price) { this.price = price ; this.title = title ; } public string tostring() { return this.title + "\t" + this.price ; } } public class testdemo { public static void main(string [] args) throws exception { ser(); // 实现序列化 dser(); // 实现反序列化 } public static void ser() throws exception { objectoutputstream oos = new objectoutputstream(new fileoutputstream(new file("f:" + file.separator + "demo" + file.separator + "byte.txt"))); oos.writeobject(new book("java开发",110.1));// 输出 oos.close(); } public static void dser() throws exception { objectinputstream ois = new objectinputstream(new fileinputstream (new file("f:" + file.separator + "demo" + file.separator + "byte.txt"))); object obj = ois.readobject();// 按照object读取 book book = (book) obj;// 向下转型 system.out.println(book); ois.close(); } }
transient 关键字
通过序列化和反序列化的code实现,我们发现:序列化操作时是将整个对象的所有属性内容进行保存;但是如果某些属性的内容不需要被保存就可以通过 transient 关键字定义。
private transient string title;
由定义可知,title属性不可以被序列化操作。
总结
不是所有的类都需要被序列化,只有需要传输的对象所在的类才需要序列化对象。
上一篇: Photoshop制作逼真的古典留声机
下一篇: python 嵌套字典取值增强版
推荐阅读
-
C#实现XML与实体类之间相互转换的方法(序列化与反序列化)
-
java 序列化对象 serializable 读写数据的实例
-
详解java中面向对象设计模式类与类的关系
-
Python3.5 Json与pickle实现数据序列化与反序列化操作示例
-
序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询
-
Java序列化与反序列化三连问:是什么?为什么要?如何做?
-
你真的了解java序列化吗
-
jquery.param()实现数组或对象的序列化方法
-
java Bean与json对象间的转换实例讲解
-
Java 对象序列化与反序列化