编写一个程序,要求该程序展示了序列化和反序列化过程。
- 提问者网友:活着好累
- 2021-05-04 08:46
- 五星知识达人网友:酒者煙囻
- 2021-05-04 09:20
import java.io.*;
import java.io.Serializable;
class Student implements Serializable
{
String sName;
int sAge;
String sSex;
public Student(String sName,int sAge,String sSex)
{
this.sName=sName;
this.sAge=sAge;
this.sSex=sSex;
}
}
public class ObjectDemo
{
public static void main(String[] args) throws Exception
{
Student s1=new Student("学生甲",18,"男");
Student s2=new Student("学生乙",19,"男");
Student s3=new Student("学生炳",20,"女");
ObjectOutputStream ob=new ObjectOutputStream(new FileOutputStream("4.txt"));
ob.writeObject(s1);
ob.writeObject(s2);
ob.writeObject(s3);
ObjectInputStream obs=new ObjectInputStream(new FileInputStream("4.txt"));
for (int i=0;i<3 ;i++ )
{
Student ss=(Student)obs.readObject();
System.out.println("姓名"+ss.sName+" 年龄"+ss.sAge+" 性别"+ss.sSex);
}
}
}