测试代码
package ObjectIO;
import java.io.*;
public class ObjIO {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\IdeaProjects\\Basic\\Module_01\\src\\basic\\module01\\File\\c.txt"));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\IdeaProjects\\Basic\\Module_01\\src\\basic\\module01\\File\\c.txt"));
oos.writeObject(new Person(18,"ARY"));
Object o = ois.readObject();
System.out.println(o);
oos.flush();
oos.close();
ois.close();
}
}
Person类代码
package ObjectIO;
import java.io.Serializable;
public class Person implements Serializable {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = 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;
}
@Override
public String toString() {
System.out.println(name + age);
return name + age;
}
}
出现异常