1 必须实现Serializable, 才可序列化!!先学实现这个接口。
//对象序列化例子
import java.io.*;
public class Objectse
{
public static void main(String[] args)throws Exception
{
Persion p = new Persion("邹阳");
//开始对Persion进行序列化
File f = new File("D:\\Persion.txt");
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(p);
oos.close();
//反序列化,反序列化之后对Persion对象进行操作
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
Persion persion = (Persion)ois.readObject();
System.out.println(persion.name);
ois.close();
}
}
//必须实现接口才能序列化
class Persion implements Serializable
{
public Persion(String name)
{
this.name = name;
}
public String name;
}