I want the method to append a new person object according to the form each time the save button is clicked, and read the new content of the file each time the retrieve button is clicked, unfortunately it only reads the last entered person.
am now here for help

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.io.*;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    private Button save, retrieve;
    private TextField first, last;
    private Label saved, retrieved;
    private DatePicker date;
    public void initialize(URL location, ResourceBundle resources) {





    public void savePerson() {

        if (first.getText().isEmpty() || last.getText().isEmpty()) {
            saved.setText("Please enter all fields");
        } else {
            String firstName = first.getText().trim();
            String lastName = last.getText().trim();
            LocalDate birthDate = date.getValue();

        try {
            //create new instance of the newClass.
            Person person = new Person(firstName, lastName, birthDate);

            //write object to the file.
            FileOutputStream fos = new FileOutputStream("file.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
        } catch (IOException ex) {

            saved.setText("NEw person saved");



    public void retrievePeople(){
            boolean eof = true;
            //read the saved objects from the file
            FileInputStream fis = new FileInputStream("file.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);

                Person result = (Person) ois.readObject();
                System.out.println(result.getFirstName() + "  " + result.getLastName() + "  " + result.getDateOfBirth());

                eof = false;

        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (ClassNotFoundException e) {
        retrieved.setText("Data retrieved");

When you write the new Person to the output file you replace/overwrite the previous contents of the file, so the file only ever contains the last Person.
You can't simply append to the file because an object file has a header, so you end up with multiple headers scattered through the file and it doesn't work.

This old DaniWeb topic shows one possible solution, but IMHO it's a bit of a fudge and may break in the future.

A better solution (in my opinion) is to have a List<Person>. When the program starts, read that list as a single object from the object file. Append new persons to the list, then write the whole list (as a single object) back to the file. (You will need to deal with the very first run where the file won't exist yet - just leave the list empty.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.