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

时间:2018-09-20 18:01:16

标签: java file object 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();
    }
}

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

1 个答案:

答案 0 :(得分:1)

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

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

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