永发信息网

编写一个程序,要求该程序展示了序列化和反序列化过程。

答案:1  悬赏:10  手机版
解决时间 2021-05-04 14:05
  • 提问者网友:活着好累
  • 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);
}
}
}

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯