Java IOException

vish234

Mitglied
Ich habe Probleme mit meinem Code. Wenn ich readObject in meinem Code verwende, erhalte ich eine IOException. Die Anwendung wird normal ausgeführt, aber wenn ich versuche, readObject zu verwenden, erhalte ich eine Ausnahme; Hier ist der Code, den ich zum Speichern von Objekten verwende:

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

und hier ist der Code, den ich zum Wiederherstellen von Objekten verwende:

Java:
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;

und das Ergebnis in der Konsole:

Java:
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)
 

KonradN

Super-Moderator
Mitarbeiter
Du hast leider die exakte Exception nicht genannt - zwar IOException erwähnt, aber ich vermute, dass Du konkret eine EOFException bekommen hast (Die von IOException erbt):

Laut Dokumentation unter https://docs.oracle.com/en/java/jav...e/java/io/ObjectInputStream.html#readObject() wird am Ende des Streams kein null Objekt ausgegeben! Du verlangst, dass ein Objekt gelesen wird und wenn der Stream am Ende ist, dann bekommst Du eine EOFException.

Diese Schleife ist so also nicht zulässig, die Du da geschrieben hast. Und Du solltest ja auch genau ein Objekt in der Datei haben und das solltest Du gezielt lesen.
 

vish234

Mitglied
Unfortunately you did not mention the exact exception - IOException mentioned, but I suspect that you have actually received an EOFException ( The one inherited from IOException ):

According to documentation under https://docs.oracle.com/en/java/jav...e/java/io/ObjectInputStream.html#readObject() no null object is output at the end of the stream! You request that an object be read and if the stream is at the end you will get an EOFException.

So this loop is not allowed that you wrote there. And you should have exactly one object in the file and you should read that specifically.
Oh ja du hast recht
Ich danke Ihnen für Ihre Hilfe
 

Ähnliche Java Themen

Neue Themen


Oben