0

我的代码有问题。当我在代码中使用 readObject 时,我得到 IOException。整个程序正常工作,但是当我想使用 readObject 时,我得到了这个异常,这是我用来保存对象的代码:

        File f = new File("employees.obj");
    ObjectOutputStream objOut = null;

    try {

        objOut = new ObjectOutputStream(new BufferedOutputStream(
                new FileOutputStream(f)));
        objOut.writeObject(newEmployee);
        objOut.flush();

        System.out.println("Object is serialized.");

    } catch (FileNotFoundException e) {
        System.out.println("File not found!");

    } catch (IOException e) {
        System.out.println("Failed!");

    } finally {

        if (objOut != null) {
            try {

                objOut.close();
            } catch (IOException e) {
            }
        }
    }

这是我用于恢复对象的代码:

    File f = new File("employees.obj");
    ObjectInputStream objIn = null;
    ArrayList<Employee> c = new ArrayList<Employee>();
    try {
        objIn = new ObjectInputStream(new BufferedInputStream(
                new FileInputStream(f)));
        while (objIn.readObject() != null) {
            Person employee = (Person) objIn.readObject();
            System.out.println("hello");
            System.out.println(employee.toString());
        }
        System.out.println(c.toString());
        return c;

    } catch (FileNotFoundException e) {
        System.out.println("1");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        System.out.println("3");
    } catch (ClassCastException e) {
        System.out.println("4");
    } finally {

        if (objIn != null) {
            try {
                objIn.close();
            } catch (IOException e) {
                System.out.println("4");
            }
        }
    }
    return c;

和控制台中的结果:

at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2553)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1296)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at org.bihe.DeSerializer.deSerializeEmployees(DeSerializer.java:20)
at org.bihe.Main.enterAsManager(Main.java:238)
at org.bihe.Main.menu(Main.java:92)
at org.bihe.Main.main(Main.java:50)
4

3 回答 3

3
    while (objIn.readObject() != null) {

将反序列化一个对象(人)。然后是下一行:

        Person employee = (Person) objIn.readObject();

尝试反序列化下一个对象。如果您位于文件的末尾(EOF),那么它会抛出 IOException.

要解决此问题,请执行以下操作:

    Person employee;
    while ((employee = (Person)objIn.readObject()) != null) {
        System.out.println("hello");
        System.out.println(employee.toString());
    }

whilereadObject()与 null 比较并将其分配给employee.

于 2013-06-07T23:49:18.853 回答
0

您已经在 while 循环测试中读取了该对象。

    while (objIn.readObject() != null) {
        Person employee = (Person) objIn.readObject();
        System.out.println("hello");
        System.out.println(employee.toString());
    }

将其更改为:

    for (Person employee = (Person) objIn.readObject(); employee != null; Person employee = (Person) objIn.readObject()) {
        System.out.println("hello");
        System.out.println(employee.toString());
    }
于 2013-06-07T23:51:58.533 回答
0

除了阅读两次的根本问题之外,您还有另一个问题。EOFException 仅表示您已到达流的末尾。抓住它,然后跳出循环。目前,您的代码错误地假定 readObject() 在流结束时返回 null。它没有。如果你写了一个空值,它会返回空值,这可能随时发生,或者永远不会发生,完全取决于你写到流中的内容。

于 2013-06-08T00:46:39.653 回答