2

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;
}}
1
  • have you checked the contents of data.ser in some file explorer? Commented Dec 10, 2016 at 18:33

1 Answer 1

3
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.

Sign up to request clarification or add additional context in comments.

1 Comment

No. ObjectOutputStream doesn't append anything. The FileOutputStream does. And the OP doesn't write persons. He writes List<Person>. So the file ends up containing two serialized List<Person>. One containing 5 elements, and a second one containing 10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.