搞了一个小时没搞出来,请高手过来看看
scott 2008-10-09 09:27:01 import java.io.*;
public class TestExternalizable {
public static void main(String[] args) {
Person p=new Person("ZYP",23);
try {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("G:\\1.txt"));
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("G:\\1.txt"));
oos.writeObject(p);
Object o=ois.readObject();
System.out.println(o);
oos.close();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class Person implements Externalizable {
String name;
int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("读出");
this.name=in.readUTF();
in.readUTF();
this.age=in.readInt();
}
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("写入");
out.writeUTF("name");
out.writeUTF("------------");
out.write(age);
}
public String toString() {
return "name="+name;
}
}
请问怎么会出错,如何解决错误,谢谢