ObjectInputStream throws EOFException

Bin ich, eine Klasse zu schreiben, um zu verfolgen Wettbewerb der Daten auf einem Turnier. Ich möchte speichern Sie diese Klasse in einer Datei, also bin mit einem ObjectInputStream. Die Klasse, die ich Schreibe, implementiert Serializable. Ich bin immer ein EOFException, und keine der Lösungen die ich gefunden habe, auf SO und anderswo tatsächlich dieses Problem zu beheben.

Mein file writer ist:

public void writeToFile(String path) {
    File f = new File(path);
    if(f.exists()) f.delete();

    try {
        OutputStream fileOut = new FileOutputStream(path);
        OutputStream bufferOut = new BufferedOutputStream(fileOut);
        ObjectOutput output = new ObjectOutputStream(bufferOut);

        output.writeObject(this);
    } catch(IOException e) {}
}

Meine Datei-reader ist:

public static DivisionDataFTC readFromFile(String path) {
    try {
        InputStream fileIn = new FileInputStream(path);
        InputStream bufferIn = new BufferedInputStream(fileIn);
        ObjectInput input = new ObjectInputStream(bufferIn);

        System.out.println(input.read());
    } catch(Exception e) {
        System.out.println(path);
        e.printStackTrace();
    }

    if(1==1) throw new Error("Could not read DivisionDataFTC at " + path);
    return null;
}

Schreibe ich die Daten erfolgreich - ich habe überprüft, dass die Datei nicht leer. (Inhalt, falls relevant, sind konsequent 7.99 kb).

Klar zu sein, tritt der Fehler nicht auf die Instanziierung der ObjectOutputStream. Dies ist, was macht diese Frage anders - der Fehler tritt auf der readObject () - Aufruf. Meine Ausgabe ist eine bemerkenswert lange EOFException:

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2571)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at java.util.ArrayList.readObject(ArrayList.java:733)
    (...cut out most of this because nobody wants to read it...)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at tournamentftc.DivisionDataFTC.readFromFile(DivisionDataFTC.java:297)
    at firstscouting.FIRSTScouting.runGUI(FIRSTScouting.java:82)
    at firstscouting.FIRSTScouting.main(FIRSTScouting.java:101)
Exception in thread "main" java.lang.Error: Could not read DivisionDataFTC at C:\Users\Noah\Desktop\out.ser
    at tournamentftc.DivisionDataFTC.readFromFile(DivisionDataFTC.java:303)
    at firstscouting.FIRSTScouting.runGUI(FIRSTScouting.java:82)
    at firstscouting.FIRSTScouting.main(FIRSTScouting.java:101)

Ich bin mir nicht sicher, warum dies geschieht. Wie umgehe ich diese?

InformationsquelleAutor Aza | 2013-03-02

Schreibe einen Kommentar