标签:
本次讲解中我们建立一个Java的项目去体会一下序列化Serializable的使用,序列化的原理以及序列化的自定义请参见我的另外一篇博客(java高级---->Serializable序列化的源码分析)
目录导航
一、 持久化的简单介绍:
“持久化”意味着对象的“生存时间”并不取决于程序是否正在执行——它存在或“生存”于程序的每一次调用之间。通过序列化一个对象,将其写入磁盘,以后在程序再次调用时重新恢复那个对象,就能圆满实现一种“持久”效果。
二、 语言里增加了对象序列化的概念后,可提供对两种主要特性的支持:
三、 Serializable的一些说明:
四、 序列化的步骤:
五、 反序列化的步骤:
项目结构如下,源代码下载见huhx友情链接:
一、 首先我们建立一个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方法添加以上两个方法的运行,结果如下:
首先我们看一下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方法添加以上两个方法的运行,结果如下:
标签:
原文地址:http://www.cnblogs.com/huhx/p/serializable.html