java中使用ObjectInputStream的readObject()获取文件中信息都为空

连滚带爬的菜鸟 连滚带爬的菜鸟 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
//Person类
public class Person implements Serializable{
private String name;
private int age;
private char gender;
private double salary;
private List<String> otherInfo;

public Person(String string, int i, char c, int j, List<String> list) {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public List<String> getOtherInfo() {
return otherInfo;
}
public void setOtherInfo(List<String> otherInfo) {
this.otherInfo = otherInfo;
}
@Override
public String toString() {
return name + "," + age + "," + gender + "," + salary + ","+ otherInfo;
}

}

//写入order.txt
public class writeDemo {
public static void main(String[] args) throws IOException {
List<String> list = new ArrayList<String>();
list.add("家住上海");
list.add("汉族");
list.add("其它信息");
Person p = new Person("战三",22,'男',2000,list);
System.out.println("开始序列化...");
FileOutputStream fos = new FileOutputStream("order.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//将list和Person信息写入文件中
oos.writeObject(p);
System.out.println("序列化完成...");
oos.close();

}
}

//读取order.txt
public class Readtest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("order.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Person P = (Person)ois.readObject();
System.out.println(P); //???没有读取到数据
ois.close();
}
}

最终我Readtest 读到的是如下的结果,我是哪个位置写错了吗?求大神指教一下,非常感谢!
null,0,口,0.0,null
...全文
评论 6
收藏 已收藏
分享
返回顶部
连滚带爬的菜鸟
Java 51,407 成员 86,133 内容