1

I create series of objects out of Student Class and store them in a vector. I write each object in to a .ser file once it created. Then I read them back. My code is working perfectly. What I want to know is, the way I do this is correct or are there any easy and optimized ways to get this done?? And also how to replace or delete specific object in a .ser file without re-witting whole the file again.

Student Class

public class Student implements Comparable<Student>, Serializable{

    private String firstName = "";
    private int registrationNumber;
    private int coursework1;
    private int coursework2;
    private int finalExam;
    private double moduleMark;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public int getRegistrationNumber() {
        return registrationNumber;
    }
    public void setRegistrationNumber(int registrationNumber) {
        this.registrationNumber = registrationNumber;
    }
    public int getCoursework1() {
        return coursework1;
    }
    public void setCoursework1(int coursework1) {
        this.coursework1 = coursework1;
    }
    public int getCoursework2() {
        return coursework2;
    }
    public void setCoursework2(int coursework2) {
        this.coursework2 = coursework2;
    }
    public int getFinalExam() {
        return finalExam;
    }
    public void setFinalExam(int finalExam) {
        this.finalExam = finalExam;
    }
    public double getModuleMark() {
        return moduleMark;
    }
    public void setModuleMark(double moduleMark) {
        this.moduleMark = moduleMark;
    }
    public int compareTo(Student s){
        if (this.moduleMark > s.moduleMark)
            return 1;
        else if (this.moduleMark == s.moduleMark)
            return 0;
        else 
            return -1;
    }
} 

File writing part

public static void Write(Student mm){
        try
          {
            FileOutputStream fileOut = new FileOutputStream("info.ser",true);
            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fileOut));

            out.writeObject(mm);            
            out.close();
            fileOut.close();
            System.out.println("Serialized data is saved in info.ser");
          }catch(IOException i)
          {
              //i.printStackTrace();
          }

    }

Reading part

public static int Read() {
        int count=0;
        try{
            vector = new Vector<Student>();
            FileInputStream saveFile = new FileInputStream("info.ser");
            ObjectInputStream save;
            try{
                for(;;){
                    save = new ObjectInputStream(saveFile);
                    student = (Student) save.readObject();
                    vector.add(student);
                    count++;
                }
            }catch(EOFException e){
                //e.printStackTrace();
            }
            saveFile.close(); 

        }catch(Exception exc){
            exc.printStackTrace();
        }
        return count;
    }
1
  • This site is not for working code. You should post this question to codereview.stackexchange.com instead. Commented Nov 23, 2013 at 12:15

1 Answer 1

1

There is no way to delete a Student object without rewriting the whole file. The reason is that you wil corrupt the header if you try to append more Student objects. Hence when you read the objects back you will get a StreamCorruptedException.

So you will have to read the objects back, delete the required object, delete the old file and write a new one.

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

2 Comments

Ah is that so.. use serializing and deserializing methods for this kind of places are good or bad practice??
I dont see a problem with writing objects to file. Vector is itself Serializable so you can write the Vector to the file in one method call. Writing the objects one by one will cause the stream to be corrupted

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.