序列化:内存中的Java对象<——>二进制流
目的:a)有时候需要把对象存储到外部存储器中持久化保存,b)还有时候,需要把对象通过网络传输。
可序列化的对象,Java要求可序列化的类实现下面两个接口之一。
——Serializable:接口只是一个标记性的接口,实现该接口无需实现任何方法;——Externalizable实现该接口需要实现方法。
序列化的IO流:
ObjectInputStream ——负责从二进制流“恢复”对象-->从文件中提取对象;ObjectOutputStream ——负责将内存中的对象写入磁盘
举例说明1(注意:一定要实现Serializable接口):
- public class Test {
- public static void main(String[] args) {
- Apple apple = new Apple("Xx苹果", "红色", 2.3);
- // System.out.println(apple);
- // 这里利用了JDK7里面的try()自动关闭资源,好处是不用手动关闭oos
- try (ObjectOutputStream oos = new ObjectOutputStream(
- new FileOutputStream("f:/1.txt"));) {
- oos.writeObject(apple);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class Apple implements Serializable {
- private String name;
- private String color;
- private double weight;
- public Apple() {
- }
- public Apple(String name, String color, double weight) {
- this.name = name;
- this.color = color;
- this.weight = weight;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public double getWeight() {
- return weight;
- }
- public void setWeight(double weight) {
- this.weight = weight;
- }
- @Override
- public String toString() {
- return "Apple [color=" + color + ", name=" + name + ", weight="
- + weight + "]";
- }
- }
而读取文件中的对象就更简单了(下面省略了上面的Apple类):
- public class Test {
- public static void main(String[] args) {
- try (
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
- "f:/1.txt"));) {
- System.out.println(ois.readObject().toString());
- } catch (ClassNotFoundException | IOException e) {
- e.printStackTrace();
- }}}
引用变量的序列化机制:
A。引用变量所引用的对象的所有属性都应该是可序列化的。
B。如果要序列化的对象是之前已经序列化的,此时系统序列化一个编号。
这种序列化机制,就是为了保存磁盘里的二进制流与内存中的对象是对应的。transient:用于修饰实例成员变量(不能与static修饰符同时使用)。--用于指定被修饰的field不会被序列化。好处:比如银行卡账号、密码就不应该被序列化出来。【注意】由于static修饰的类变量存储在类信息中,并不存储在对象里,所以有static修饰的类变量不能被序列化。
自定义序列化类:
- /**
- * @author lhy
- * @description 自定义序列化类
- */
- class User implements Serializable {
- private static final long serialVersionUID = 546525067577254190L;
- private String account;
- private String password;
- public User() {
- }
- public User(String account, String password) {
- this.account = account;
- this.password = password;
- }
- public String getAccount() {
- return account;
- }
- public void setAccount(String account) {
- this.account = account;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "User [account=" + account + ", password=" + password + "]";
- }
- // 下面两个方法,提供给系统调用,系统会调用者两个方法完成实际的序列化
- private void writeObject(ObjectOutputStream out) throws IOException {
- // 序列化User的两个属性
- out.writeUTF(account);
- out.writeUTF(new StringBuilder(password).reverse().toString());
- }
- private void readObject(ObjectInputStream in) throws IOException,
- ClassNotFoundException {
- account = in.readUTF();
- password = new StringBuilder(in.readUTF()).reverse().toString();
- }
- }
- public class Test {
- public static void main(String[] args) {
- User user = new User("张三", "123");
- ObjectOutputStream oos = null;
- try {
- oos = new ObjectOutputStream(new FileOutputStream("f:/1.txt"));
- oos.writeObject(user);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- oos.close();
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- }
读取文件中的对象:
- public class Test {
- public static void main(String[] args) {
- ObjectInputStream ois = null;
- try {
- ois = new ObjectInputStream(new FileInputStream("f:/1.txt"));
- User user = (User)ois.readObject();
- System.out.println(user.toString());
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- ois.close();
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- }
运行一下,我们可以看到输出:User [account=张三, password=123]
自定义(稳定)序列化:可以借助于“定制序列化”对属性进行一些“加密”。
【版本号】当我们的类经常使用时,有时候系统无法确定“发序列化”是的class文件是否还正确。--建议显式为“可序列化”指定一个版本号。--因为系统默认的版本号不稳定(经常改变)。serialver.exe -专门用来查看类的版本号。用法:serialver 序列化的类。--当我们修改了类时,记得要修改版本号。
结束语:
有关Java中的序列化今天就讲到这里,明天开始学习Java面向对象之界面编程。