重复写入和加载文件中对象的ArrayList

时间:2016-12-10 18:30:55

标签: java file arraylist load writefile

我有一个'Person'类,我存储了名称,姓氏等数据。我制作5个对象类型Person,将它们添加到ArrayList,并将此ArrayList保存到文件中。接下来我从这个文件ArrayList加载,我有5个人。问题是我想再次保存例如10个对象Person。当我从文件加载ArrayList时,我从第一次写入时只得到5个人。如果我重复这一点,我将从首次写入此文件时加载数据。我怎么能解决这个问题?

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 个答案:

答案 0 :(得分:3)

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

您正在为true参数传递append。因此,您已经在已有的5人名单之后追加 10个人的列表。因为你只读了一个列表,所以你读了你写的第一个,其中包含5个人。

传递false而不是true。

相关问题