I am trying to create a simple program with user interface. The user from gui inserts a name, surname and age, of a class Person Implements Serializable.
Everything works just fine, but only for one person, the first person! It seems that every time I send a new object to my file it is indeed written to the file since the file size is getting larger.
The problem is that when I read the file with objects it returns just one, I want all the objects of class person that are stored in myFile to be returned.
Here is a sneak peek of my code:
Write object Button:
try {
per = new Person(name, sur, age);
FileOutputStream fos = new FileOutputStream("myFile", true);
ObjectOutputStream oos;
oos = new ObjectOutputStream(fos);
oos.writeObject(per);
oos.flush();
oos.close();
//some other code here
} catch (IOException e) {
//some code here
}
Read object button:
try {
FileInputStream fis = new FileInputStream("myFile");
ObjectInputStream ois;
ois = new ObjectInputStream(fis);
try {
rper = (Person) ois.readObject();
ois.close();
JOptionPane.showMessageDialog(null, "People:" + rper, "Saved Persons", JOptionPane.INFORMATION_MESSAGE);
} catch (ClassNotFoundException e) {
//some code here
}
} catch (IOException e) {
//some code here
}
Thanks for any info!