Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的生命周期更长。但在现实应用中,就可能要求在JVM停止运行之后能够保存(持久化)指定的对象,并在将来重新读取被保存的对象。Java对象序列化就能够帮助我们实现该功能。
使用Java对象序列化,在保存对象时,会把其状态保存为一组字节,在未来,再将这些字节组装成对象。必须注意地是,对象序列化保存的是对象的"状态",即它的成员变量。由此可知,对象序列化不会关注类中的静态变量。
package 序列化;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Person implements Serializable
{
public String toString()
{
return name+","+age; //用来system.out
}
Person(){
System.out.println("no args");
}
public Person(String name,int age){
System.out.println("hava args");
this.name=name;
this.age=age;
}
private String name=null;
private Integer age=null;
}
public class Test1
{
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException
{
// TODO Auto-generated method stub
System.out.println("hello1");
File f=new File("person.txt");
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(f));
Person p=new Person("jia",12);
out.writeObject(p); //把信息写入
out.close();
ObjectInputStream in=new ObjectInputStream(new FileInputStream(f));
Object p1=in.readObject(); //读取信息 文件f可以传输,只要保证有Person.class
in.close(); 文件即可 String和数组和枚举类型默认序列化 没有
System.out.println(p1); 调用构造函数
}
}
在现实应用中,有些时候不能使用默认序列化机制。比如,希望在序列化过程中忽略掉
敏感数据,可以在变量前加上transient,这样序列化时将忽略这个字段
方法2:实现Externalizable接口
package 序列化;
import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Person1 implements Externalizable
{
public String toString()
{
return name+","+age;
}
public Person1(){
System.out.println("no args");
}
public Person1(String name,int age){
System.out.println("hava args");
this.name=name;
this.age=age;
}
private String name;
transient private Integer age;
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException
{
// TODO Auto-generated method stub
name=(String)in.readObject();
age=in.readInt();
}
@Override
public void writeExternal(ObjectOutput out) throws IOException
{
// TODO Auto-generated method stub
out.writeObject(name);
out.writeInt(age);
}
}
public class Test2
{
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException
{
// TODO Auto-generated method stub
System.out.println("hello1");
// File f=new File("person.txt");
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("h1.txt"));
Person1 p=new Person1("jia",12);
out.writeObject(p);
out.close();
ObjectInputStream in=new ObjectInputStream(new FileInputStream("h1.txt"));
Object p1=in.readObject();
in.close();
System.out.println(p1);
}
}
使用Externalizable进行序列化时,当读取对象时,会调用被序列化类的无参构造器去创建一个新的对象,然后再将被保存对象的字段的值分别填充到新对象中。这就是为什么在此次序列化过程中Person类的无参构造器会被调用。由于这个原因,实现Externalizable接口的类必须要提供一个无参的构造器,且它的访问权限为public。