javaarraylistobjectinputstream

ObjectOutputStream, readObject only reads first object from serialized file


I have an ArrayList of Objects and i want to store them into the file and also i want to read them from the file to ArrayList. I can successfully write them into the file using writeObject method but when reading from the file to ArrayList, i can only read first object. Here is my code for reading from serialized file

 public void loadFromFile() throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        myStudentList = (ArrayList<Student>) ois.readObject();
}

EDIT:

This is the code for writing list into the file.

 public void saveToFile(ArrayList<Student> list) throws IOException {
        ObjectOutputStream out = null;
        if (!file.exists ()) out = new ObjectOutputStream (new FileOutputStream (file));
        else out = new AppendableObjectOutputStream (new FileOutputStream (file, true));
        out.writeObject(list);
}

Rest of my class is

public class Student implements Serializable {
    String name;
    String surname;
    int ID;
    public ArrayList<Student> myStudentList = new ArrayList<Student>();
    File file = new File("src/files/students.txt");


    public Student(String namex, String surnamex, int IDx) {
        this.name = namex;
        this.surname = surnamex;
        this.ID = IDx;
    }

    public Student(){}

    //Getters and Setters


    public void add() {

        Scanner input = new Scanner(System.in);


        System.out.println("name");
        String name = input.nextLine();
        System.out.println("surname");
        String surname = input.nextLine();
        System.out.println("ID");
        int ID = input.nextInt();
        Ogrenci studenttemp = new Ogrenci(name, surname, ID);
        myOgrenciList.add(studenttemp);
        try {
            saveToFile(myOgrenciList, true);
        }
        catch (IOException e){
            e.printStackTrace();
        }


    }

Solution

  • Ok so you are storing whole list of students every time when new student comes in, so basicly what your file is keeping is:

    1. List with one student
    2. List with two students including the first one
    3. List of 3 studens
    4. and so on and so on.

    I know you are probably thought it will write only new students in incremental fashion, but you were wrong here .

    You should rather add all students you want to store, into the list first. And then store complete list into the file , just like you are doing it.

    Now, when you will be reading from the filre, first readObject will return you the list no.1 - that is why you are getting list with only one student. Second read would give you list no.2 and so on.

    So you save your data you either have to:

    1. Create complete list containig N students and store it once ito the file
    2. Do not use list, but store students directly to the file

    To read it back:

    1. readObject once, so you will get List<Students>
    2. Read students one by one from the file by multiple calls to readObject