IOException in ObjectInputStream in java

守給你的承諾、 提交于 2019-12-12 01:38:29

问题


I have problem with my code. I get IOException when I use readObject in my code. the whole program work correctly but when I want to use readObject I get this exception, this is the code I use for saving object:

        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) {
            }
        }
    }

and it is the code I use for restoring object:

    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;

and the result in console:

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)

回答1:


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

will deseralize one object (Person). Then the next line:

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

attempts to deseralize the next object. If you're at the end of the file (EOF), then it throws IOException.

To fix this do something like this:

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

The while compares readObject() with null and assigns it to employee.




回答2:


You already read the object in the while loop test.

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

Change it to:

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



回答3:


In addition to the underlying problem of reading twice, you have another problem. EOFException just means you've reached the end of the stream. Catch it, and break out of the loop. At present your code incorrectly assumes that readObject() returns null at end of stream. It doesn't. It returns null if you wrote a null, which can happen any time, or never, depending entirely in what you wrote to the stream.



来源:https://stackoverflow.com/questions/16994291/ioexception-in-objectinputstream-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!