【问题标题】:Serialized object java序列化对象java
【发布时间】:2014-05-03 12:02:05
【问题描述】:

今天我开始在 java 中使用序列化对象,我是新手,在尝试反序列化时遇到了一些问题。 我有这个文件,我在其中编写了所有 Account 对象,我猜它写得很好。问题是我不知道如何从该文件中引用特定对象,或者如何将它们全部放入列表中?然后参考它。 这就是我试图阅读它们的方式:

public void readAccount(Account e) {
/*  List<Account> results = new ArrayList<Account>();
    try {
        FileInputStream fileIn = new FileInputStream("test.txt");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        for (int i = 0; i < accBank.size(); i++) {
            results.add((Account) in.readObject());
        }
        in.close();
        fileIn.close();
    } catch (IOException i) {
        i.printStackTrace();
        return;
    } catch (ClassNotFoundException c) {
        System.out.println("Employee class not found");
        c.printStackTrace();
        return;
    }
    for (Account acc : results) {
        System.out.println(toString(acc));
        if(e.getAcc_no() == acc.getAcc_no())
        {System.out.println("Deserialized Account...");
        System.out.println(toString(e));
        }
    }
    */
        List<Account> results = new ArrayList<Account>();
        Account acc = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("test.txt");
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(fis);
                results.add((Account) ois.readObject());
                acc = (Account) ois.readObject();
            }
        } catch (Exception ignored) {
            // as expected
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        }
        System.out.println("results = " + results);

        for (Account ac : results) {
            System.out.println(toString(ac));
            if(e.getAcc_no() == ac.getAcc_no())
            {System.out.println("Deserialized Account...");
            System.out.println(toString(e));
            }
    }
}

这就是我写它们的方式:

public void writeAccount(Account e) {
    try {
        ObjectOutputStream os1 = new ObjectOutputStream(
                new FileOutputStream("test.txt", true));
        os1.writeObject(e);
        os1.close();
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}

编辑:

public void writeFile() {
    for (int i = 0; i < accBank.size(); i++) {
        writeAccount(retAcc(i));
    }
}

谁能告诉我我做错了什么?我还尝试了其他问题的其他示例,但没有奏效。

【问题讨论】:

    标签: java serialization deserialization


    【解决方案1】:

    您做错的是您使用多个 ObjectOutputStream 写入同一个文件(不是 txt 文件,顺便说一句,因为它包含二进制数据),并使用单个 ObjectInputStream 来读取所有帐户。因此,每次写入帐户时都会写入一个新的序列化标头,而 ObjectInputStream 不会这样。

    编写帐户列表的最佳方法就是这样做:将帐户存储到List&lt;Account&gt;,然后编写列表。要读取帐户列表,您只需执行以下操作:从文件中读取单个对象,并将其转换为 List&lt;Account&gt;

    【讨论】:

    • 请检查“编辑:”部分。
    • 编辑部分证实了我的回答:您有一个帐户列表,并为每个帐户使用新的 OOS 分别编写每个帐户。只需将列表直接写入您的文件,然后阅读列表。不需要任何循环。或者至少,使用唯一的 OOS 来编写每个帐户,并像现在一样继续读取文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    相关资源
    最近更新 更多