如何实现可序列化?
2022-09-02 12:29:10
我应该如何界面?implement
Serializable
我有一个,并且需要能够将其保存到磁盘。对于我的家庭作业,我必须序列化五个不同的对象并将它们保存到文件中。class
Student
Student
class Student {
String mFirstName;
String mSecondName;
String mPhoneNumber;
String mAddress;
String mCity;
Student(final String pFirstName, final String pSecondName, final String pPhoneNumber, final String pAddress, final String pCity){
this.mFirstName = pFirstName;
this.mSecondName = pSecondName;
this.mPhoneNumber = pPhoneNumber;
this.mAddress = pAddress;
this.mCity = pCity;
}}
我尝试使用 来序列化一个 ,但它抛出了一个错误:ObjectOutputStream
Student
ObjectOutputStream lOutputStream = new ObjectOutputStream(new FileOutputStream("file.txt", true));
lOutputStream.write(new Student("foo","bar","555-1234","Flat 40","Liverpool"));