public class TestExternalizable implements Externalizable {
@Override
public void writeExternal(ObjectOutput out) throws IOException { }
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { }
public static void main(String[] args) throws IOException, ClassNotFoundException {
A a = new A();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e:/test/b.txt"));
oos.writeObject(a);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:/test/b.txt"));
TestExternalizable aa = (TestExternalizable)ois.readObject();
ois.close();
}
}
class A extends TestExternalizable{
// public A(){
//
// }
}
如果在类A中不写无参构造器,就会报错:Exception in thread "main" java.io.InvalidClassException: com.test.A; no valid constructor