1

I am trying to create a new .ser file to store objects if there is not one already present. When this is ran, it throws an EOFException. What exactly is an EOFException and is this method correctly written to create and read a .ser file? Thanks for any feedback.

public void readDatabase() throws IOException {

        File dataFile = new File("database.ser");

        // If data file does not exist, create it.
        if (!dataFile.exists()) {
            System.out.println("database.ser does not exist, creating one now . . .");
            // if the file doesn't exists, create it        
            dataFile.createNewFile();
            return; // No need to try to read anything from an empty file, so return.
        }
        ObjectInputStream objectinputstream = null;
        boolean cont = true;
        try {
            FileInputStream streamIn = new FileInputStream(dataFile);
            objectinputstream = new ObjectInputStream(streamIn);
            while (cont) {
                Item obj = null;
                try {
                    obj = (Item) objectinputstream.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                if (obj != null)
                    itemList.add(obj);
                else
                    cont = false;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (objectinputstream != null) {
                objectinputstream.close();
            }
        }

    }

EOFException:

java.io.EOFException
    at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2758)
    at java.base/java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3253)
    at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:866)
    at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:343)
    at hardwarestore.HardwareStore.readDatabase(HardwareStore.java:254)
    at hardwarestore.HardwareStore.<init>(HardwareStore.java:33)
    at hardwarestore.MainApp.<init>(MainApp.java:24)
    at hardwarestore.MainApp.main(MainApp.java:259)

1 Answers1

0

EOFException stands for:

End of File Exception

This typically occurs when you try to read data from a file where the FileReader has reached the end of the file. In other words, no more data to read.

You should catch the exception and close your stream. as it indicates that you have read all the objects in the file. Referring to the answer in this question:

 while (true) {
        try {
            // Read the next object from the stream. If there is none, the
            // EOFException will be thrown.

            Item obj = (Item) objectinputstream.readObject();
            itemList.add(obj);
        } catch (EOFException e) {
            // If there are no more objects to read, return what we have.
            return contactMap;
        } finally {
            // Close the stream.
            in.close();
        }
    }
TM00
  • 1,330
  • 1
  • 14
  • 26