Jacob Schweiger
Jacob Schweiger

Reputation: 1

Why am I getting an end of file exception when trying to use serialization on a hash map in java?

    public static void updateNumberOfEpsWatched() throws IOException, ClassNotFoundException
    {
        String currentDirectory = System.getProperty("user.dir");
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("Progress/HashMapStorage.ser"));
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Progress/HashMapStorage.ser"));
        HashMap<String,Integer> mapToWrite = new HashMap<>();
        mapToWrite.put("one", 1);
        out.writeObject(mapToWrite);
        HashMap<String, Integer> hashMap = (HashMap<String, Integer>) in.readObject();
        out.close();
        in.close();
        System.out.println(hashMap);
    }

The program is able to write a hashmap to a file and then read it back properly but if I comment out the "out.writeObject(mapToWrite);" then it can't read back the hashmap even though the file is still there. I am trying to make it so that the hashmap can be stored by the program before the program ends and then read back when the user launched the program so that it's data persists multiple uses of the program.

I was expecting to be able to comment out the "out.writeObject(mapToWrite);" and then it would be able to read the file written to before. I have tried closing the object writer but it made no difference. There error code was:

Exception in thread "main" java.lang.RuntimeException: java.io.EOFException
    at Ch01_11_2024_progress.main(Ch01_11_2024_progress.java:15)
Caused by: java.io.EOFException
    at java.base/java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3228)
    at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1711)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:538)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:496)
    at Ch01_11_2024_progress.updateNumberOfEpsWatched(Ch01_11_2024_progress.java:27)
    at Ch01_11_2024_progress.main(Ch01_11_2024_progress.java:10)

Upvotes: -1

Views: 73

Answers (1)

BerZZyy
BerZZyy

Reputation: 9

The issue in your code is related to how you are handling the ObjectOutputStream and ObjectInputStream. When you create the ObjectOutputStream, it writes a header to the file, and when you close it, it writes a trailer. If you don't write any objects in between, the file might end up being empty or contain only the header and trailer.

Here's the corrected version of your code:

public static void updateNumberOfEpsWatched() throws IOException, ClassNotFoundException {
    String currentDirectory = System.getProperty("user.dir");

    // Writing to the file
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Progress/HashMapStorage.ser"));
    HashMap<String, Integer> mapToWrite = new HashMap<>();
    mapToWrite.put("one", 1);
    out.writeObject(mapToWrite);
    out.close();

    // Reading from the file
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("Progress/HashMapStorage.ser"));
    HashMap<String, Integer> hashMap = (HashMap<String, Integer>) in.readObject();
    in.close();
    
    System.out.println(hashMap);
}

In this corrected version, I removed the unnecessary read operation after the ObjectOutputStream was closed and added a separate read section after closing the ObjectInputStream. This way, you ensure that the file is properly closed after writing before attempting to read from it. The original issue occurred because the file might not have been completely written when you tried to read it without closing the ObjectOutputStream.

Upvotes: 0

Related Questions