-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.

2 Answers2

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.

alibttb
  • 526
  • 4
  • 19
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.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • 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. – Abhishek Hedaoo Sep 21 '19 at 14:33