我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;
}}
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用ObjectOutputStream序列化a Student,但它会抛出一个错误:
ObjectOutputStream lOutputStream = new ObjectOutputStream(new FileOutputStream("file.txt", true));
lOutputStream.write(new Student("foo","bar","555-1234","Flat 40","Liverpool"));
Run Code Online (Sandbox Code Playgroud)