Skip to content
Advertisement

Serialization process constantly overwrites itself?

I’m fairly new to java and trying to do some serialization in my project. I have a bunch of objects called Student and I would like to serialize them. The code I’m using for this is as follows:

  try{
                    for(Student s: students){ 
                    FileOutputStream fileOut = new FileOutputStream("C:/Users/Jaimee-Lee/Desktop/Advanced programming work/MilestoneOne/student.ser");
                    ObjectOutputStream out = new ObjectOutputStream(fileOut);
                    out.writeObject(s);
                    out.close();
                    fileOut.close();
                    }
                    System.out.printf("Serialized data is saved in /MilestoneOne/student.ser n");
                    

                }catch(IOException i){
                    i.printStackTrace();
                }

The issue I’m finding is that the students array I’m using has twenty students (S1 – S20). When I attempt to deserialize the objects, it only gives me the object that contains the last student to have been serialized (S20)

This is my deserialization code:

for(Student student : students){
                System.out.println(student.getStudentID());
            }

            try(FileInputStream fis = new FileInputStream("student.ser");
                    ObjectInputStream ois = new ObjectInputStream(fis)){
                        while(fis.available() > 0 ){
                            deserializedStudents.add((Student) ois.readObject());
                            System.out.println("Added");
                        }

                        System.out.println(Arrays.toString(deserializedStudents.toArray()));
                        deserializedStudents.forEach(student -> System.out.println(student.getStudentID()));


                    }catch(IOException | ClassNotFoundException exc){
                        exc.printStackTrace();
                    }

I have also noticed that when I open the .ser file, that there is only one line in it. I’m assuming this may be evidence that it is in fact overwriting itself every time as my understanding is that there should be as many lines as there objects in the serialization file.

Can anyone help me to understand what I am doing that is causing the file to overwrite itself instead of retaining objects that have already been added?

Also, here is my Student class for reference:

import java.io.Serializable;

public class Student implements Serializable{
    
    private static final long serialVersionUID = 1L;

    // Class Attributes
    private String studentID;
    private String rankings;
    private char personalityType;
    private String conflict;
    
    private String preferences;

    
    // Class Constructor

    public Student(String ID){
        this.studentID = ID;
    }
    
    public Student(String ID, String grades) {
        this.studentID = ID;
        grades = grades.trim();
        this.rankings = grades;
    }

    public Student(String ID, String ranking,char personality){
        this.studentID = ID;
        this.rankings = ranking;
        this.personalityType = personality;
    }
    
    // Accessor Methods
    public String getStudentID() {
        return this.studentID;
    }

    public String getRankings(){
        return this.rankings;
    }

    public String getPreferences(){
        return this.preferences;
    }

    public char getPersonalityType(){
        return this.personalityType;
    }

    public String getConflict(){
        return this.conflict;
    }

    //Modifier Methods

    public boolean setPreferences(String pref){
        this.preferences = pref;
        return true;
    }

    public boolean setGrades(String grades){
        this.rankings = grades;
        return true;
    }

    public boolean setPersonalityType(char pers){
        this.personalityType = Character.toUpperCase(pers);
        return true;
    }

    public boolean setConflict(String ids){
        this.conflict = ids;
        return true;
    }

    @Override

    public String toString(){
        return studentID + ";" + rankings + ";" + personalityType + ";" + conflict + ";" + preferences; 
    }
    

    
}

Advertisement

Answer

for(Student s: students){ 
    FileOutputStream fileOut = new FileOutputStream("C:/Users/Jaimee-Lee/Desktop/Advanced programming work/MilestoneOne/student.ser");
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(s);
    out.close();
    fileOut.close();
}

You’re creating a new FileOutputStream inside the for-loop, overwriting the old information with each iteration. Don’t do this, create the stream before the loop and use it within the loop:

FileOutputStream fileOut = new FileOutputStream("C:/Users/Jaimee-Lee/Desktop/Advanced programming work/MilestoneOne/student.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for(Student s: students){ 
    out.writeObject(s);
}
out.close();

And close it after the loop.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement