为什么输出是null

ljs34101130 ljs34101130 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
package Externalizable;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class Person implements Externalizable {

private String name;
private int age;
public Person(){

}

public Person(String name, int age){
this.name = name;
this.age = age;
}

public String toString(){
return "姓名: " +this.name + " 年龄" + this.age;
}
//只写或读 下面的东西
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(this.name);
out.writeObject(age);
}
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
this.name = (String)in.readObject();
this.age = in.readInt();
}
}
-----------------------------------------------------------------------------------
package Externalizable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import Transirnt.Person;

public class Externalizabletest {

public static void main(String[] args) {

ser();
dser();

}

private static void ser() {

File f = new File("e:/" + File.separator + "tes.txt");
try {
ObjectOutputStream oos = null;
OutputStream out = new FileOutputStream(f);
oos = new ObjectOutputStream(out);
oos.writeObject(new Person("lkkj", 12));
// System.out.println();
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

private static void dser() {
try {
File f = new File("e:/" + File.separator + "tes.txt");
ObjectInputStream ois = null;
InputStream input = new FileInputStream(f);
ois = new ObjectInputStream(input);
Object obj = ois.readObject();
ois.close();
System.out.println(obj);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}
}
求大神帮助。。。
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
ljs34101130
Java 51,407 成员 86,128 内容