【问题标题】:Appending in Serialization在序列化中追加
【发布时间】:2021-03-29 16:18:56
【问题描述】:

到目前为止,我发现有一种方法可以通过创建子类来进行序列化附加,但这似乎是一个漫长的方法。有没有更好的方法在序列化中追加?

我在一个名为 Course

的类中有这个 vectorlist

private Vector<Student> StudentList = new Vector<>();

我有 2 个课程对象。 3 名学生注册了一门课程,2 名学生注册了另一门课程。现在我调用这个在文件中进行序列化的方法,但是当我用我的第二个课程对象调用它时,它会替换以前的内容。

public void Serialization() {
        try {
            File file = new File("EnrolledStudentsSerial.txt");
            if(!file.exists()){
               file.createNewFile();
            }
            FileOutputStream fo = new FileOutputStream(file);
            ObjectOutputStream output = new ObjectOutputStream(fo);
            output.writeObject("Course: " + this.name + "\n\nEnrolled Students: ");
            for (int i = 0; i < StudentList.size(); i++) {
                Student p_obj = StudentList.elementAt(i);
                String content = "\n\tStudent Name: " + p_obj.getName() + "\n\tStudent Department: " + p_obj.getDepartment() + "\n\tStudent Age: " + p_obj.getAge() + "\n";
                output.writeObject(content);
            }
            output.writeObject("\n");
            fo.close();
        } catch (IOException ioe){
            System.out.println("Error: " + ioe.getMessage()); 
        }
    }

【问题讨论】:

    标签: java serialization netbeans objectoutputstream


    【解决方案1】:

    如果你想追加到一个文件,而不是替换内容,你需要告诉FileOutputStream,通过添加一个额外的参数并调用FileOutputStream(File file, boolean append)仅供参考:不要使用ObjectOutputStream

    您不需要调用createNewFile(),因为FileOutputStream 会这样做,无论是否附加。

    但是,您实际上并没有序列化您的对象,因为您正在序列化字符串。你在做什么没有意义。由于您似乎希望结果是文本文件(您正在编写文本,并且文件名称为 .txt),因此您应该忘记 ObjectOutputStream,而改用 FileWriter

    更好的是,您应该使用 Java 7 中添加的“较新”的 NIO.2 API,而不是使用旧的 File I/O API。您还应该使用 try-with-resources。古老的 Vector 类也是如此,它在 Java 1.2 中被 ArrayList 取代。

    Java 命名约定是字段和方法名称以小写字母开头。而且由于您的方法不再“序列化”,因此您应该给它一个更好的名称。

    应用所有这些,您的代码应该是:

    import static java.nio.file.StandardOpenOption.APPEND;
    import static java.nio.file.StandardOpenOption.CREATE;
    import static java.nio.file.StandardOpenOption.WRITE;
    
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    
    private ArrayList<Student> studentList = new ArrayList<>();
    
    public void writeToFile() {
        Path file = Paths.get("EnrolledStudentsSerial.txt");
        try (BufferedWriter output = Files.newBufferedWriter(file, CREATE, APPEND, WRITE)) {
            output.write("Course: " + this.name + "\n\nEnrolled Students: ");
            for (Student student : studentList) {
                String content = "\n\tStudent Name: " + student.getName() +
                                 "\n\tStudent Department: " + student.getDepartment() +
                                 "\n\tStudent Age: " + student.getAge() + "\n";
                output.write(content);
            }
            output.write("\n");
        } catch (IOException ioe){
            System.out.println("Error: " + ioe.getMessage()); 
        }
    }
    

    【讨论】:

    • 我实际上是 Java 的新手,我正在为我的大学作业做这个,所以我知道如何附加到文件,但是这段代码会写一个普通的文本,对吧?那么如何序列化呢?
    • @HarisMusa 为什么要对 text 进行 Java 序列化?这没有任何意义。
    • 所以我已经研究了我的概念,现在我明白这些对象应该是序列化的,但我还有 2 个问题。 1. 如何在同一个文件中附加 2 个对象,例如先序列化 1 个对象,然后再序列化另一个。 2. 序列化有什么好处,就像我只是尝试序列化一个对象然后反序列化它,但是序列化后我能用它做什么?好处和用途?
    • @HarisMusa 1) 不应附加到 Java 序列化文件。 2)你问的是序列化的目的吗?请在网上进行一些研究。这个问题有很多答案。
    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 2019-09-09
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    相关资源
    最近更新 更多