我应该如何实现 Serializable
接口?
我有一个名为 Student
的 class
,需要能够将其保存到磁盘。为了完成我的作业,我必须对五个不同的 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"));