-1

I am trying to deserialze some file in java using ObjectInputStream. I am able to read all the objects successfully but getting the EOFException. Couldn't able to find the way to check for EOFException. Tried below code. Any suggestions are appreciated.

Thanks for the help

    List<Destination> destinationList = new ArrayList<Destionation>();
    try(ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName))) {
      Destination destination = null;
            while(true) {
                destination = (Destination) in.readObject();
                if(destination == null)
                    break;

                destinationList.add(destination);
            }

            // Some code here

    } catch (Exception e) {
        e.printStackTrace();
    }

**Note :- ** I only have the serialzed file.

1
  • Please provide the stack trace and provide code for serialization also Commented Sep 21, 2019 at 13:57

2 Answers 2

0

The EOFException means that your ObjectInputStream has reached the file end while reading through a serialized object, this means a corrupted file in most cases, or you are still reading after your ObjectInputStream has reached eof, which seems to be your case considering the way you wrote your code, any way you can check for it by declaring it in the catch clause:

    List<Destination> destinationList = new ArrayList<Destionation>();
    try(ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName))) {
      Destination destination = null;
            while(true) {
                destination = (Destination) in.readObject();
                if(destination == null)
                    break;

                destinationList.add(destination);
            }

            // Some code here

    } catch (EOFEception eofEx){
        eofEx.printStackTrace();
    }catch (Exception e) {
        e.printStackTrace();
    }

But I don't think this is what you're looking for, what you need is a way to read objects without this problem, you can do this:

    List<Destination> destinationList = new ArrayList<Destionation>();
    try(ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName))) {
      Destination destination = null;
            while(true) {
               try{
                   destination = (Destination) in.readObject();
                   destinationList.add(destination);
               }catch (EOFEception eofEx){
                   break;
               }
            }

            // Some code here

    } catch (Exception e) {
        e.printStackTrace();
    }

by any means, the result would be the same.

Sign up to request clarification or add additional context in comments.

Comments

0

ObjectInputStream.readObject does not return null for end of input.

For a null terminated list of objects, you can write a null at the end of the file with ObjectOutputStream.writeObject.

1 Comment

Thanks for responding. But O am not the one who created this seriralize file. Though I will try your solution for while creating any other serialize file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.