3
\$\begingroup\$

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;
        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;
    }
\$\endgroup\$
  • 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
5
\$\begingroup\$

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

            for(;;){
                save = new ObjectInputStream(saveFile);
    

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

            save = new ObjectInputStream(saveFile);
            for(;;){
    
  • 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.

| improve this answer | |
\$\endgroup\$
  • 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.