import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializableOperator
{
public static void main(String args[]) throws IOException
{
SerializableObject so=new SerializableObject();
so.setAge(20);
so.setName("snow");
FileOutputStream fos=new FileOutputStream("c:/test/test.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(so);
oos.close();
FileInputStream fis=new FileInputStream("c:/test/test.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
try {
SerializableObject soo=(SerializableObject)ois.readObject();
ois.close();
System.out.print(soo.getAge()+" "+soo.getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class SerializableObject implements Serializable//对象的序列化,并取回
{
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
IO知识复习五
最新推荐文章于 2023-02-25 16:48:43 发布