//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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/113538.html
標籤:Java相關
上一篇:為什么第二個while回圈是從score=6開始的?
下一篇:Python字串詳解
