package gatherandgenericity;
import java.io.*;
import java.util.Date;
public class TestObjectStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Person p = in();
Person p2 = out();
System.out.println(p);
System.out.println(p2);
}
public static Person out() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("d:/Object.txt",false));
Person p = new Person("张大",22000,50);
oos.writeObject(p);
oos.flush();
oos.close();
return p;
}
public static Person in() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("d:\\Object.txt"));
Person p = (Person)ois.readObject();
ois.close();
System.out.println(p.toString());
return p;
}
}
package gatherandgenericity;
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private double sal;
private int age;
public Person(){
super();
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sal=" + sal +
", age=" + age +
'}';
}
public Person(String name, double sal, int age) {
this.name = name;
this.sal = sal;
this.age = age;
}
}