JAVA串行化(序列化)
假设你想保存一个或多个对象的状态。如果Java不具备串行化能力,就不得不使用某个I/O类写出你想保存的所有对象的实例变量的状态。
串行化简单地说就是“
保存这个对象及其所有实例变量Transient,这意味,不会把瞬态变量的值作为对象的串行化状态的一部分包括进来。”。实际上,更有趣的是,显式地将一个变量标识为
将要实现序列化的类必须实现Serializable接口或者是Externalizable接口。
在Serializable接口中没有定义任何方法,而在Externalizable接口中需要实现
public void writeExternal(ObjectOutput out) throws IOException{}以及
public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException{}抽象方法。
一.实现Serializable接口的序列化
/**
*功能:个人信息类
* @author zhangqi
*/
package demo;
import java.io.Serializable;
public class Person implements Serializable
{
private String name;
private char sex;
private int age;
public Person(String name,char sex,int age)
{
this.name=name;
this.sex=sex;
this.age=age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public char getSex()
{
return sex;
}
public void setSex(char sex)
{
this.sex = sex;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String toString()
{
return getName()+", "+getSex()+", "+getAge();
}
}
/**
*功能:实现对象序列化
* @author Administrator
*/
package demo;
import java.io.*;
public class SerializeTest {
public static void writeO(Person p)
{
try
{
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("test.txt"));
oos.writeObject(p);
oos.close();
System.out.println("写入完成!!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void readO()
{
Person p=null;
try
{
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("test.txt"));
p=(Person)ois.readObject();
System.out.println("读出输出");
System.out.println(p);
ois.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Person p1 = new Person("张琦",'男',22);
writeO(p1);
readO();
}
}
当然对于一些敏感数据,不应该将它序列化输出,我们可以在属性前面加上关键字transient,可以让相应的属性不被序列化。
二.实现Externalizable接口实现序列化
在这个类中实现Externalizable接口的两个用于读写属性的方法readExternal()和writeExternal()。通过实现这两个方法,就可以自定义对象的序列化机制。
当从保存了序列化的对象的媒质中读出对象的时候,它会调用类的不带任何参数的默认构造器来构造对象,因此,如果在这个实现Externallizable接口的类中定义类构造器,那么还需要定义一个不带参数的构造器。
/**
* ExternExample.java
*/
package demo;
import java.io.*;
/**
* @author zhangqi
*/
public class ExternExample implements Externalizable{
private String name;
private String creditcard;
private int age;
private double salary;
public ExternExample() {
System.out.println("没有参数的构造器被使用!");
}
public ExternExample(String name,String creditcard,int age,double salary)
{
this.name=name;
this.creditcard=creditcard;
this.age=age;
this.salary=salary;
}
public void writeExternal(ObjectOutput out) throws IOException
{
System.out.println("写入对象...");//本处未对creditcard属性进行序列化。
out.writeObject(name);
out.writeInt(age);
out.writeDouble(salary);
}
public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException
{
System.out.println("读出对象...");
name=(String)in.readObject();
age=in.readInt();
salary=in.readDouble();
}
public String toString()
{
return name+" ,"+age+" ,"+salary;
}
}
/*
* SerializeTest.java
*
*/
package demo;
import java.io.*;
/**
*
* @author zhangqi
*/
public class SerializeTest {
public static void writeO(ExternExample p)
{
try
{
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("test.txt"));
oos.writeObject(p);
oos.close();
System.out.println("写入完成!!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void readO()
{
ExternExample p=null;
try
{
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("test.txt"));
p=(ExternExample)ois.readObject();
System.out.println("读出输出");
System.out.println(p);
ois.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ExternExample e1 = new ExternExample("张琦","110107",22,10000);
writeO(e1);
readO();
}
}