我创建了一个为图书馆系统注册成员的程序。为了存储我使用文本文件的数据并读/写我实现了序列化。
我的问题是当我注册一个成员并尝试注册另一个成员时,第二个成员记录将不会被保存。为了将写入附加到文件中,我也使用了关键字“true”。
这是我所做的:
//Am writing the object to a file
public void RegisterMembers(Members object) {
try{
FileOutputStream write=new FileOutputStream("Memberships.txt",true);
ObjectOutputStream writeFile=new ObjectOutputStream(write);
writeFile.writeObject(object);
writeFile.flush();
writeFile.close();
}catch(Exception e){
Frame frame=new Frame();
JOptionPane.showMessageDialog(frame,"Storage file not found");
}
}
//Method to read from the file
public void readMemberships() throws FileNotFoundException, IOException, ClassNotFoundException{
Person object;
FileInputStream read=new FileInputStream("Memberships.txt");
ObjectInputStream readFile=new ObjectInputStream(read);
object=(Person)readFile.readObject();
System.out.println(object);
}
//Here is a piece of code which is in the gui frame which i implemented
Members newMember=new Members(firstName,initials,birthDay,birthMonth,birthYear,Gender,memberid,address,tele,membershipType,paymentType);
newMember.RegisterMembers(newMember); //calls the 2 methods
newMember.readMemberships();
这里的错误似乎是什么?我希望它在写作时附加。
感谢您的时间。