Home > Back-end >  In Java using the ObjectInputStream readObject () get information in the file is empty
In Java using the ObjectInputStream readObject () get information in the file is empty

Time:09-23

//the Person class
Public class Person implements the Serializable {
private String name;
private int age;
Private char gender;
Private double salary;
Private List OtherInfo;

Public Person (String String, int, char, c int j, List The list) {
Super ();
}
Public String getName () {
return name;
}
Public void elegantly-named setName (String name) {
this.name=name;
}
Public int getAge () {
Return the age;
}
Public void setAge (int age) {
this.age=age;
}
Public char getGender () {
Return the gender;
}
Public void setGender (char gender) {
This. Gender=gender;
}
Public double getSalary () {
Return the salary;
}
Public void setSalary (double salary) {
This. Salary=salary;
}
Public List GetOtherInfo () {
Return otherInfo;
}
Public void setOtherInfo (List OtherInfo) {
Enclosing otherInfo=otherInfo;
}
@ Override
Public String toString () {
Return the name + + ", "age + + +", "gender", "+ +", "salary + otherInfo;
}

}

//write order. TXT
Public class writeDemo {
Public static void main (String [] args) throws IOException {
List List=new ArrayList (a);
List. The add (" Shanghai ");
List. The add (" han ");
List. The add (" other information ");
Person p=new Person (" three ", 22, 'male', 2000, a list).
System. Out.println (" start serialization... ");
FileOutputStream fos=new FileOutputStream (" order. TXT ");
The ObjectOutputStream oos=new ObjectOutputStream (fos);
//the list and the Person information written to the file
Oos. WriteObject (p);
System. Out.println (" serialization to complete... ");
Oos. The close ();

}
}

//read the 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. The out. Println (P);//??? No data read
Ois. Close ();
}
}

Eventually I read Readtest is the result of the following, is where I write wrong? O great god teach, thank you very much!
, null, 0, 0.0, and null

CodePudding user response:

System. Out.println (" serialization to complete... ");
oos. The flush (); //add try here, prevent data in the output stream buffer, also not completely written to the file
Oos. The close ();

CodePudding user response:

Uh-huh, a small detail, another is the Person didn't assignment constructor, plus normal after the thank you thank you!!!!!!

CodePudding user response:

reference 1st floor qybao response:
System. Out.println (" serialization to complete... ");
oos. The flush (); //add try here, prevent data in the output stream buffer, also not completely written to the file
Oos. The close ();

Uh-huh, a small detail, another is the Person didn't assignment constructor, plus normal after the thank you thank you!!!!!!
  • Related