0

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();
        }


    }
9
  • And where is the code writing the list? Provide a complete minimal test case. If the read list contains only one element, the only possible explanation is that the written list contained only one element. Commented Dec 16, 2015 at 18:51
  • We'll need to see also the code with which you write the data. Commented Dec 16, 2015 at 18:52
  • I added the code, sorry about that. Commented Dec 16, 2015 at 19:01
  • Your list has only one element. Commented Dec 16, 2015 at 19:48
  • @Antoniossss i check the list before writing to the file and list has several elements. Commented Dec 16, 2015 at 19:52

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! That is exactly what was going wrong. I'll use 1st solution i guess. Thanks again.
@FikriCanCankurtaran sure no problem
0

This is Because I think ObjectOutputStream will return the first object from a file. If you want all of the objects you can use for loop and use like this -:

    FileInputStream fis = new FileInputStream("OutObject.txt");

    for(int i=0;i<3;i++) {
        ObjectInputStream ois = new ObjectInputStream(fis);
        Employee emp2 = (Employee) ois.readObject();

        System.out.println("Name: " + emp2.getName());
        System.out.println("D.O.B.: " + emp2.getSirName());
        System.out.println("Department: " + emp2.getId());
    }

Comments

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.