java基础---->Serializable的使用
本次讲解中我们建立一个Java的项目去体会一下序列化Serializable的使用,序列化的原理以及序列化的自定义请参见我的另外一篇博客(java高级---->Serializable序列化的源码分析)
目录导航
Serializable序列化的简要说明
一、 持久化的简单介绍:
“持久化”意味着对象的“生存时间”并不取决于程序是否正在执行——它存在或“生存”于程序的每一次调用之间。通过序列化一个对象,将其写入磁盘,以后在程序再次调用时重新恢复那个对象,就能圆满实现一种“持久”效果。
二、 语言里增加了对象序列化的概念后,可提供对两种主要特性的支持:
- 远程方法调用(RMI)使本来存在于其他机器的对象可以表现出好象就在本地机器上的行为。将消息发给远程对象时,需要通过对象序列化来传输参数和返回值。
- 使用一个Java Bean 时,它的状态信息通常在设计期间配置好。程序启动以后,这种状态信息必须保存下来,以便程序启动以后恢复;具体工作由对象序列化完成。
三、 Serializable的一些说明:
- 对象的序列化处理非常简单,只需对象实现了Serializable 接口即可(该接口仅是一个标记,没有方法)
- 序列化的对象包括基本数据类型,所有集合类以及其他许多东西,还有Class 对象
- 对象序列化不仅保存了对象的“全景图”,而且能追踪对象内包含的所有句柄并保存那些对象;接着又能对每个对象内包含的句柄进行追踪
- 使用transient关键字修饰的的变量,在序列化对象的过程中,该属性不会被序列化。
四、 序列化的步骤:
- 首先要创建某些OutputStream对象:OutputStream outputStream = new FileOutputStream("output.txt")
- 将其封装到ObjectOutputStream对象内:ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
- 此后只需调用writeObject()即可完成对象的序列化,并将其发送给OutputStream:objectOutputStream.writeObject(Object);
- 最后不要忘记关闭资源:objectOutputStream.close(), outputStream .close();
五、 反序列化的步骤:
- 首先要创建某些OutputStream对象:InputStream inputStream= new FileInputStream("output.txt")
- 将其封装到ObjectInputStream对象内:ObjectInputStream objectInputStream= new ObjectInputStream(inputStream);
- 此后只需调用readObject()即可完成对象的反序列化:objectInputStream.readObject();
- 最后不要忘记关闭资源:objectInputStream.close(),inputStream.close();
Serializable序列化的代码实例
项目结构如下,源代码下载见huhx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MRjRnDVAy6U6Omn8ZN+xA+P2OhRpzvJUu8pI7jqARdh5emHHgCYKmHl6QfBAFMlrDz9IBhgqsDyfgCQAQgGAGQAggEAGfw/1qGHRP6xmEYAAAAASUVORK5CYII=" alt="" />
一、 首先我们建立一个Man类,实现了Serializable接口,用于Person类的测试:
package com.huhx.model;
import java.io.Serializable; public class Man implements Serializable {
private static final long serialVersionUID = 1L; private String username;
private String password; public Man(String username, String password) {
this.username = username;
this.password = password;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}
二、 我们再建立一个Person类,用于序列化:
package com.huhx.model;
import java.io.Serializable; public class Person implements Serializable {
private static final long serialVersionUID = 1L; private Man man;
private String username;
private transient int age; public Person() {
System.out.println("person constru");
} public Person(Man man, String username, int age) {
this.man = man;
this.username = username;
this.age = age;
} public Man getMan() {
return man;
}
public void setMan(Man man) {
this.man = man;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
三、 编写一个包含main方法的测试类:MainTest,它的writeSerializableObject用于序列化对象:
// Serializable:把对象序列化
public static void writeSerializableObject() {
try {
Man man = new Man("huhx", "123456");
Person person = new Person(man, "刘力", 21);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("output.txt"));
objectOutputStream.writeObject("string");
objectOutputStream.writeObject(person);
objectOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
四、 测试类MainTest,它的readSerializableObject用于反序列化对象:
// Serializable:反序列化对象
public static void readSerializableObject() {
try {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("output.txt"));
String string = (String) objectInputStream.readObject();
Person person = (Person) objectInputStream.readObject();
objectInputStream.close();
System.out.println(string + ", age: " + person.getAge() + ", man username: " + person.getMan().getUsername());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
五、 在Main方法添加以上两个方法的运行,结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATcAAAAQCAIAAAD8nq9fAAACn0lEQVRoge1a27HFIAhMXRZkPVZjMxbj/fAFBojmmJiZy36diQZhYSM654gKheLbOHY7oFAoLqAqVSi+DlWpQvF1PKJSbw/jwhOWP4jgzJFg/W5fFDGWjIxnY3b++7hWqTNf9n83vD2Mqz+VqY8gTFbt7PyX0VRat4TDGJMqr20T/W6RBowLZUoeqW/AvTR/q6w9mYkx+vLUOGe7FydQzfQWGPswtCIz/MpQ1rwFq91LdfLEmLxo9rcYouIS+WRXSJOSPUgRkffI8kPmXfLn7L8c76K8BGes65scgQdyfoRBZj/39IhH9aeu722XGIaXEmj6DSdBa/UJJKuOOoMovM+B93X9ZpO3Dx0MzuCC+EGlfWGNobLjba37vDwTF8ensEKLBzjN5V3ih8o76w/pPx/vqrzAbEN/BB7I+dmJgCN8HXUvBR897IygUq44SJUS7OCyHqk2Fnjbh/VC2D/1CLf5X6bSUvTJkUYXGRdfbcIKzHwq7zI/1HKsfTkvXbzr8iKoceo5CGHnTQt1LgVnrfiiSkeqjQG6rkIlTtv39p6cqIVbXLcPN6xKmbhWqhQHk2kR+ZlQ6VVezvEuystKlZbv2MaDa1YpkhW+BmlDpT+p035VKV639BbAjUFuoBpxZ8LZF2+hJzqrGJwFNtE7N/w/7y1MXDdUCvsIkn8YgMTPuEo5//neYVVerjt8zIPYCbeT7K79tKqUbzP8+VYAtklwoH8ObxcOdAonbnGMQdr2M3dJ0Iy14GKdt8+HPKNSaIegbcB/yE55p95zkHFJfF4ThG7RhLxTQ3TeBX8I/50U75K8jNVb44Gbf4fnZ/ChfzV0O3C/ta62/zSW+6/4t9ivUvC1BWeSAG80H7D/NNb5r1DsV6lCoZChKlUovo4/sqKBsijdi+4AAAAASUVORK5CYII=" alt="" />
- 在Person类中包含Man的引用,当Person被序列化的时候,从结果可以知道Man也被序列化了
- writeObject方法可以传入String,是因为String首先是一个类,其次它也是实现了Serializable接口的
- Person类中的age字段是transient,从打印结果可以看到,序列化Person person = new Person(man, "刘力", 21)对象时,age没有进行序列化。如果transient修饰的Object类型的,那么打印的结果将会是null
Externalizable序列化的代码实例
首先我们看一下Externalizable的定义:继承了Serializable接口
public interface Externalizable extends java.io.Serializable
一、 同样的我们先创建一个实现了Externalizable的User类:重写里面两个方法readExternal和writeExternal
package com.huhx.model; import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput; public class User implements Externalizable {
private String user; public String getUser() {
return user;
} public int getAge() {
return age;
} private int age; public User() {
System.out.println("user constructor.");
} public User(String user, int age) {
System.out.println("user constructor two.");
this.user = user;
this.age = age;
} @Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("read external.");
user = (String) in.readObject();
age = in.readInt();
} @Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("write external.");
out.writeObject(user);
out.writeInt(age);
}
}
二、 在MainTest中加入方法writeExternalizableObject,用于序列化对象User
// Externalizable的序列化对象
public static void writeExternalizableObject() {
User user = new User("huhx", 22);
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("Externalizable.txt"));
objectOutputStream.writeObject(user);
objectOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
三、 在MainTest中加入方法writeExternalizableObject,用于反序列化对象User
// Externalizable的反序列化对象
public static void readExternalizableObject() {
try {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("Externalizable.txt"));
User user = (User) objectInputStream.readObject();
objectInputStream.close();
System.out.println("name: " + user.getUser() + ", age: " + user.getAge());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
四、 在Main方法添加以上两个方法的运行,结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQwAAABTCAIAAADlQa5SAAAF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PGB70KmK0QCDmS6ulSmK/wnV4RMV412P5/pCpFckZ/4ChjgCIgEwAGRADggEgAHRALggEgAHBAJgAMiAXBAJAAObxVJnn+vfmcTn4OMVj/Lu+8kvS+T3wUZreCTyPwkyzRZm4GsDFVW5idRPuYdIiGjFVyb8k6imYFMc5We+enpjfRktIJ/Ti2SegQ0zFXqLteqBpnWQ4WMVnB5PJGYDxVG5qdekZDRCi6PKxLDXGVmfirsTpXXXIWMVnBxVpG0zECqn8nI/FRZzeO+x5K6fjJawXXgx0QAB0QC4IBIABwQCYADIgFwQCQADogEwAGRADggEgCHi4pE5gl5R/n/hXjv6rjnOJzLRUUyaZsLzy3/XxB70oq3dFvH4XTKTFc7TUhz6RCWDUtLAc+ktdW164KOKcS0VlVmutIyUanlTfPWC5hmrPWD8gXbJ/WDxJoI7jpBXIQi9UKHCWkt/ZjF1uvUMj91ikSOt0IMRkYutXzDvNWHYcaq+kRNBXGkH4qzLNvCXV4kcE12JPFRTUjrlvLH1vjl9Lb5qYeGGLqOiz/h2NK93Q/b/55uI8f6QbauVHFQ+bADVySGCckSyXmz2pkiMcxbPXj9ULd75uxepWh0j8O5eCKxTEimSJrmp95nEkMMaiaq5jJMN291hGObsap0wUZmrIqOfrCMa3sMbXAKxYN7+YBrm5CSKL287GGUj9R20qm9g6NlutIyUVnl2+atHIf9M7FpMpMfhMK3eLwfzLfDNN4aA2dz3a+AP0CRRfoMcMDfkh8Wyfj8wqAXEbcS1j835IdFArAPRALggEgAHBAJgAMiAXD4A/DfhRmr35NkAAAAAElFTkSuQmCC" alt="" />
- 首先User user = new User("huhx", 22);执行了User的含参构造函数
- 当执行到writeObject(user);方法时,由于User实现了Externalizable接口,所以它的writeExternal会执行,
- 在User中的readExternal方法中调用了ObjectInput的readObject方法,在这个方法中通过反射机制创建User的实例,调用了User的无参构造函数。
- 然后在readObject方法执行的时候,同样会先执行User类的readExternal方法。这个会在后续源代码分析时讲到
友情链接
- 本文测试代码: 访问密码 12bb
- 序列化的原理分析 java高级---->Serializable序列化的源码分析
java基础---->Serializable的使用的更多相关文章
- java高级---->Serializable的过程分析
本次讲解中我们在上次的基础上,深入的了解一下序列化的流程以及其中的原理.关于序列化的一些知识与使用,请参见我的另一篇博客:java基础---->Serializable的使用.好了,我们进行以下 ...
- java后端知识点梳理——java基础
面向对象 java三大特性 封装: 将事务封装成一个类,达到解耦,隐藏细节的效果.通过get/set等方法,封装了内部逻辑,并保留了特定的接口与外界联系. 继承: 从一个已知的类中派生出一个新的类,新 ...
- Java基础知识强化104:Serializable接口 与 Parcelable接口
1. 什么是 序列化 和 反序列化 ? 序列化 :序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间.序 ...
- Java基础--序列化Serializable
对Java对象序列化的目的是持久化对象或者为RMI(远程方法调用)传递参数和返回值. 下面是一个序列化对象写入文件的例子: ---------------------------- package u ...
- java基础 序列化反序列化流 实现Serializable 接口 自动装载序列号到对象文本文件如修改不能反序列化对象文本,除非自定义long型常量 打印流
package com.swift.baseKnowledge; import java.io.File; import java.io.FileInputStream; import java.io ...
- Java基础(十一)--Serializable和Externalizable接口实现序列化
序列化在日常开发中经常用到,特别是涉及到网络传输的时候,例如调用第三方接口,通过一个约定好的实体进行传输,这时你必须实现序列 化,这些都是大家都了解的内容,所以文章也会讲一下序列化的高级内容. 序列化 ...
- JAVA基础知识|Serializable
一.序列化和反序列化 序列化:把对象转换为字节序列的过程称为对象的序列化. 反序列化:把字节序列恢复为对象的过程称为对象的反序列化. 将内存中对象的信息保存下来,可以有很多种方式实现这一功能.其中ja ...
- 【JAVA面试题系列一】面试题总汇--JAVA基础部分
JAVA基础 基础部分的顺序: 基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法 线程的语法,集合的语法,io 的语法,虚拟机方面的语法 每天几道,持续更新!! 1.一个". ...
- 最适合作为Java基础面试题之Singleton模式
看似只是最简单的一种设计模式,可细细挖掘,static.synchronized.volatile关键字.内部类.对象克隆.序列化.枚举类型.反射和类加载机制等基础却又不易理解透彻的Java知识纷纷呼 ...
随机推荐
- UDAF(用户自定义聚合函数)求众数
除了逐行处理数据的udf,还有比较常见的就是聚合多行处理udaf,自定义聚合函数.类比rdd编程就是map和reduce算子的区别. 自定义UDAF,需要extends org.apache.spar ...
- [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Centos开机自启动脚本的制作
原文地址:http://www.2cto.com/os/201306/220559.html 我的一个Centos开机自启动脚本的制作 一.切换到/etc/init.d/ 二.制作sh脚本 v ...
- C# 调用C++的dll 那些事
之前从来没搞过C++,最近被安排的任务需要调用C++的接口,对于一个没用过 Dependency 的小白来说,原本以为像平时的Http接口那样,协议,端口一定义,方法参数一写就没事,结果踩了无数的坑. ...
- SpringMvc HandlerMethodResolver 的 handlerMethods & ServletHandlerMethodResolver 的 mappings 在哪里初始化的 ?
HandlerMethodResolver 的 handlerMethods & ServletHandlerMethodResolver 的 mappings 在哪里初始化的 ? 如下图:
- vue mand-mobile ui加class不起作用的问题 css权重问题
vue mand-mobile ui加class不起作用的问题 css权重问题组件的样式优先权比自己定的class高多加几层权重才行,要直接用样式覆盖也可以在前面多加几层class,定位更精确了才会覆 ...
- Internet spirit
互联网思维精髓总结为 :1.用户思维:2.简约思维:3.极致思维:4.迭代思维:5.流量思维:6.社会化思维:7.大数据思维:8.平台思维:9.跨界思维.
- JS判断元素是否在数组内
//判断元素是否在数组内 function contains(arr, obj) { var i = arr.length; while (i--) { if (arr[i] === obj) { r ...
- Linux+Jenkins环境搭建
一.安装基础环境 1. yum -y install java-1.8.0-openjdk.x86_64 #安装1.8jdk 2. 查看java 版本 [root@localhost djanggo_ ...
- java框架注意
struts2 数据类型不匹配时会return "input" <result name="input">/WEB-INF/index.jsp< ...