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, I do this in a correct optimized way 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;
            return -1;

File writing part

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

            System.out.println("Serialized data is saved in info.ser");
          }catch(IOException i)


Reading part

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

        }catch(Exception exc){
        return count;

closed as off-topic by Jamal Jun 16 '14 at 7:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ your very last question is off topic, we won't tell you how to write a block of code. \$\endgroup\$ – Malachi Nov 23 '13 at 14:32

Why didn't you provide a constructor for Student class? Was that on purpose? However...

  • Use serialVersionUID.

  • In Java it's recommended not to use Vector class, instead use ArrayList.

  • Write method is serializing a Student individually. But as you stated you are writing a whole list of student into the .ser file. This will take many time for a list of 10,000 students.

    Because you are opening two streams for each student. So instead pass the whole list and serialize them after opening the stream only once.

  • Do not close the stream in the try block. close them into finally block. Even better if you are using Java7 or higher use try-with-resources block. It is easy to use and you don't have to manually close the stream.

  • In Read method

                save = new ObjectInputStream(saveFile);

    you are creating new stream for every object thats not necessary instead do this

            save = new ObjectInputStream(saveFile);
  • Onwards Java7 there is multiple catch statement. Give it a try(pun intended).

  • In Java we use mixed-case convention for naming a method. So Write and Read would be write and read.

  • 1
    \$\begingroup\$ I'm new to java and means a lot ur quick reply. learnt a lot. Thanks for ur time :) \$\endgroup\$ – Eclayaz Nov 23 '13 at 13:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.