简体   繁体   English

使用ObjectOutputStream将一系列对象写入.ser文件并读回它们的最佳方法

[英]best way to write series of objects to a .ser file using ObjectOutputStream and read them back

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. 创建后,我将每个对象写入.ser文件。 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. 以及如何替换或删除.ser文件中的特定对象,而无需再次重新整个文件。

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

There is no way to delete a Student object without rewriting the whole file. 如果不重写整个文件,则无法删除Student对象。 The reason is that you wil corrupt the header if you try to append more Student objects. 原因是,如果尝试附加更多的Student对象,则会损坏标头。 Hence when you read the objects back you will get a StreamCorruptedException . 因此,当您读回对象时,将得到StreamCorruptedException

So you will have to read the objects back, delete the required object, delete the old file and write a new one. 因此,您将不得不读回这些对象,删除所需的对象,删除旧文件并编写一个新文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM