Search code examples
javafilearraylistloadwritefile

Repeated Writing and Loading ArrayList of objects from a file


I have a 'Person' class where i stored data like name, surname etc. I make 5 object type Person, add them to ArrayList, and save this ArrayList to file. Next i'm loading from this file ArrayList and i have 5 person. Problem is when i want save again for example 10 object Person. When i'm loading ArrayList from file i'm getting only 5 person from first writing. If i repeat this still i will have load data from first writing to this file. How i can fix this ?

public class Data {
static List<Person> persons = new ArrayList<Person>();

public static void main(String[] args) throws IOException {
    Data.savePersons(5);
    Data.loadPersons();

    /** Clean 'persons' array for TEST of load data */
    persons.removeAll(persons);

    System.out.println("\n-----------\nNext Round\n-----------\n");

    Data.savePersons(10);
    Data.loadPersons();
}

/** Save a couple of Person Object to file C:/data.ser */
public static void savePersons(int noOfPersonToSave) throws IOException {
    FileOutputStream fout = null;
    ObjectOutputStream oos = null;

    /** Make 5 'Person' object and add them to ArrayList 'persons' for example */
    for (int i = 0; i < noOfPersonToSave; i++) {
        Person personTest = new Person("name" + i, "surname" + i, "email" +i, "1234567890" +i);
        persons.add(personTest);
    }

    try {
        fout = new FileOutputStream("C:\\data.ser", true);
        oos = new ObjectOutputStream(fout);
        oos.writeObject(persons);

        System.out.println("Saving '" + persons.size() + "' Object to Array");
        System.out.println("persons.size() = " + persons.size());
        System.out.println("savePersons() = OK");

    } catch (Exception ex) {
        System.out.println("Saving ERROR: " + ex.getMessage());

    } finally {
        if (oos != null) {
            oos.close();
        }
    }
}

/** Load previously saved a couple of Person Object in file C:/data.ser */
public static void loadPersons() throws IOException {
    FileInputStream fis = null;
    ObjectInputStream ois = null;

    try {
        fis = new FileInputStream("C:\\data.ser");
        ois = new ObjectInputStream(fis);

        persons = (List<Person>) ois.readObject(); 
        //persons.add(result);

        System.out.println("-------------------------");
        System.out.println("Loading '" + persons.size() + "' Object from Array");
        System.out.println("persons.size() = " + persons.size());
        System.out.println("loadPersons() = OK");

    } catch (Exception e) {
        System.out.println("-------------------------");
        System.out.println("Loading ERROR: " + e.getMessage());

    } finally {
        if (ois != null) {
            ois.close();
        }
    }
}}

class Person implements Serializable {

private static final long serialVersionUID = 1L;
private String name;
private String surname;
private String mail;
private String telephone;

public Person(String n, String s, String m, String t) {
    name = n;
    surname = s;
    mail = m;
    telephone = t;
}

public String getName() {
    return name;
}

public String getSurname() {
    return surname;
}

public String getMail() {
    return mail;
}

public String getTelephone() {
    return telephone;
}}

Solution

  • new FileOutputStream("C:\\data.ser", true)
    

    You're passing true for the append parameter. So you're appending a list of 10 persons to the file, after the already existing list of 5 people. And since you only read one list, you read the first you wrote, which contains 5 persons.

    Pass false instead of true.