java中使用ObjectInputStream的readObject()获取文件中信息都为空
//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