java - 如何检查文件是否已包含对象?

标签 java file object header

我正在尝试将对象(人名、电话号码等)添加到文件中,然后读取它。当我运行程序并向文件添加一些对象时,只有第一个对象包含 header ,而第一个对象之后的对象则不包含 header ,这正是我想要的。

但是,如果我关闭程序然后重新运行它,以前的对象仍然存在并起作用,但是如果我向文件添加一个新对象,程序会将其视为“第一个对象”,因此给它一个header,当程序尝试读取对象中存储的信息时会导致错误。

这是我的“添加到文件”代码:

try {
        FileOutputStream fos = new FileOutputStream("outputfile.ser", true);
        oos = new ObjectOutputStream(fos);
    }
    catch (NotSerializableException e1) {
        System.out.println("An object was not serializable, it has not been saved.");
        e1.printStackTrace();
    }
    catch (IOException e1) {
        e1.printStackTrace();
    }


    JButton btnAddContact = new JButton("Add Contact");
    btnAddContact.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

                Address newContact = new Address(firstNameTextField.getText(), lastNameTextField.getText(), homePhoneNumberTextField.getText(), mobilePhoneNumberTextField.getText(), addressTextArea.getText(), emailTextField.getText());

                try {
                    oos.writeObject(newContact);
                }
                catch (IOException e1) {
                    System.out.println("An object was not serializable, it has not been saved.");
                    e1.printStackTrace();
                }

            dispose();
            firstNameTextField.setText("");
            lastNameTextField.setText("");
            homePhoneNumberTextField.setText("");
            mobilePhoneNumberTextField.setText("");
            addressTextArea.setText("");
            emailTextField.setText("");
        }
    });
    btnAddContact.setBounds(165, 384, 110, 46);
    contentPane.add(btnAddContact);

我还认为,如果我检查文件是否为空或不使用 if (file.length() == 0) 会起作用,但它仍然不起作用。我的代码示例:

File file = new File("outputfile.ser");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
ObjectOutputStream oas = AppendingObjectOutputStream(new FileOutputStream(file, true));
if (file.length() == 0){
                    try {
                        oos.writeObject(newContact);
                    }
                    catch (IOException e1) {
                        System.out.println("An object was not serializable, it has not been saved.");
                        e1.printStackTrace();
                    }
                } else {
                    try {
                        oas.writeObject(newContact);
                    }
                    catch (IOException e1) {
                        System.out.println("An object was not serializable, it has not been saved.");
                        e1.printStackTrace();
                    }
                }

public class AppendingObjectOutputStream extends ObjectOutputStream {

    public AppendingObjectOutputStream(OutputStream oas) throws IOException {
        super(oas);
    }

    protected void writeStreamHeader() throws IOException {
        // do not write a header, but reset:
        reset();
    }
}

有什么办法可以解决这个问题吗?

最佳答案

您的第一个 FileOutputStream fos = new FileOutputStream(file); 将覆盖任何现有文件。

首先尝试更改它FileOutputStream fos = new FileOutputStream(file, true);

但我不鼓励同时打开同一个文件。您可能需要重新设计逻辑。

关于java - 如何检查文件是否已包含对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52430793/

相关文章:

jvm - Java SE 的软件架构与 JDK、JRE 和 JVM 的关系?初学者

java - 从 Servlet 调用 Javascript 函数

javascript - 将http流写入文件

c - 如何将重定向输出写入 C 中循环内的文件?

java - 进程死亡时导航图作用域 ViewModel

java - Spring Cloud Gateway 引导失败

java - 将文件列表保存为 .txt

python - 不确定解决方案对于 python 类是否正确

java - 使用调用另一个类的 getter 并将该类作为格式化字符串返回?

java - 在Java中按值过滤Map?