import java.io.*;
public class SerializableTest
{
public static void main(String args[]) throws IOException, ClassNotFoundException
{
Person per1=new Person(20,"xiao ming",1.70);
Person per2=new Person(23,"xiao qiang",1.73);
FileOutputStream fos=new FileOutputStream("c:/out.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(per1);
oos.writeObject(per2);//将两个对象序列化放入文件中
FileInputStream fis=new FileInputStream("c:/out.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Person p;
for(int i=0;i<2;i++)
{
p=(Person)ois.readObject();
System.out.println(p.age+" "+p.name+" "+p.height);
}
ois.close();
}
}
class Person implements Serializable
{
int age;
String name;
double height;
public Person(int age,String name,double height)
{
this.age=age;
this.name=name;
this.height=height;
}
}
对对象进行序列化以及反序列化操作
最新推荐文章于 2021-02-21 18:32:20 发布