62,615
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
class Serialization
{
public static void main(String[] args) throws IOException,ClassNotFoundException
{
Student sd=new Student(19,"dintdding",20,"computer");
FileOutputStream fos=new FileOutputStream("Myfile.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
try
{
oos.writeObject(sd);
oos.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
sd=null;
FileInputStream fis=new FileInputStream("Myfile.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
try
{
sd=(Student)ois.readObject();
fis.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
System.out.println("id = "+sd.id);
System.out.println("name = "+sd.name);
System.out.println("age = "+sd.age);
System.out.println("department = "+sd.department);
}
}
class Student implements Serializable
{
int id;
String name;
int age;
String department;
public Student(int id,String name,int age,String department)
{
this.id=id;
this.name=name;
this.age=age;
this.department=department;
}
public Student()
{
}
}