源:http://www.cnblogs.com/bluesky5304/archive/2010/04/07/1706061.html;
基本概念:
序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
序列化为一般文件,也序列化为XML文件(使用XStream)
用于序列化的实体类Person.java 代码如下(记得需要实现Serializable接口):
- import java.io.Serializable;
- @SuppressWarnings("serial")
- public class Person implements Serializable{
- private String name;
- private int age;
- public Person(){
- }
- public Person(String str, int n){
- System.out.println("Inside Person's Constructor");
- name = str;
- age = n;
- }
- String getName(){
- return name;
- }
- int getAge(){
- return age;
- }
- }
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- public class SerializeToFlatFile {
- public static void main(String[] args) {
- SerializeToFlatFile ser = new SerializeToFlatFile();
- ser.savePerson();
- ser.restorePerson();
- }
- public void savePerson(){
- Person myPerson = new Person("Jay",24);
- try {
- FileOutputStream fos = new FileOutputStream("E:\\workspace\\2010_03\\src\\myPerson.txt");
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- System.out.println("Person--Jay,24---Written");
- System.out.println("Name is: "+myPerson.getName());
- System.out.println("Age is: "+myPerson.getAge());
- oos.writeObject(myPerson);
- oos.flush();
- oos.close();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- public void restorePerson() {
- try {
- FileInputStream fis = new FileInputStream("E:\\workspace\\2010_03\\src\\myPerson.txt");
- ObjectInputStream ois = new ObjectInputStream(fis);
- Person myPerson = (Person)ois.readObject();
- System.out.println("\n--------------------\n");
- System.out.println("Person--Jay,24---Restored");
- System.out.println("Name is: "+myPerson.getName());
- System.out.println("Age is: "+myPerson.getAge());
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- }
运行结果为(console输出),当然可以查看到myPerson.txt文件已经生成:
Inside Person's Constructor
Person--Jay,24---Written
Name is: Jay
Age is: 24
--------------------
Person--Jay,24---Restored
Name is: Jay
Age is: 24
控制对象的序列化和反序列化
接口 java.io.Externalizable 。当对象实现了这个接口时,就可以灵活的控制它的序列化和反序列过程,这个接口继承了java.io.Serializable
Externalizable 接口定义了两个方法,writerExternal方法在序列化时被调用,可以再该方法中控制序列化内容,readExternal方法在反序列时被调用,可以在该方法中控制反序列的内容
实例:UserInfo
- import java.io.*;
- import java.util.*;
- //本程序通过实现Externalizable接口控制对象序列化和反序列
- public class UserInfo implements Externalizable {
- public String userName;
- public String userPass;
- public int userAge;
- public UserInfo(){
- }
- public UserInfo(String username,String userpass,int userage){
- this.userName=username;
- this.userPass=userpass;
- this.userAge=userage;
- }
- //当序列化对象时,该方法自动调用
- public void writeExternal(ObjectOutput out) throws IOException{
- System.out.println("现在执行序列化方法");
- //可以在序列化时写非自身的变量
- Date d=new Date();
- out.writeObject(d);
- //只序列化userName,userPass变量
- out.writeObject(userName);
- out.writeObject(userPass);
- }
- //当反序列化对象时,该方法自动调用
- public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException{
- System.out.println("现在执行反序列化方法");
- Date d=(Date)in.readObject();
- System.out.println(d);
- this.userName=(String)in.readObject();
- this.userPass=(String)in.readObject();
- }
- public String toString(){
- return "用户名: "+this.userName+";密码:"+this.userPass+
- ";年龄:"+this.userAge;
- }
- }
- import java.io.ObjectOutputStream;
- import java.io.ObjectInputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.Date;
- import java.lang.management.*;
- public class Test {
- //序列化对象到文件
- public static void serialize(String fileName){
- try
- {
- //创建一个对象输出流,讲对象输出到文件
- ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(fileName));
- UserInfo user=new UserInfo("renyanwei","888888",20);
- out.writeObject(user); //序列化一个会员对象
- out.close();
- }
- catch (Exception x)
- {
- System.out.println(x.toString());
- }
- }
- //从文件反序列化到对象
- public static void deserialize(String fileName){
- try
- {
- //创建一个对象输入流,从文件读取对象
- ObjectInputStream in=new ObjectInputStream(new FileInputStream(fileName));
- //读取UserInfo对象并调用它的toString()方法
- UserInfo user=(UserInfo)(in.readObject());
- System.out.println(user.toString());
- in.close();
- }
- catch (Exception x)
- {
- System.out.println(x.toString());
- }
- }
- public static void main(String[] args) {
- serialize("D:\\test.txt");
- System.out.println("序列化完毕");
- deserialize("D:\\test.txt");
- System.out.println("反序列化完毕");
- }
- }
执行结果
现在执行序列化方法
序列化完毕
现在执行反序列化方法
Thu Oct 23 22:18:12 CST 2008
用户名: renyanwei;密码:888888;年龄:0
反序列化完毕