简体   繁体   中英

Why am I getting an EOF exception when de-serializing my saved objects

I am trying to serialize and then deserialize multiple objects. I can write to a file without any problems, but I get the following stack trace when selecting my deserialize option. Previously I could serialize and deserialize one object successfully.

Stack trace in dos is:

java.io.EOFException
        at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Sourc
e)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.readObject(Unknown Source)
        at mainmenutest.DeserializeDemo.Deserialize(DeserializeDemo.java:23)
        at mainmenutest.MainmenuTest.getInput(MainmenuTest.java:64)
        at mainmenutest.MainmenuTest.main(MainmenuTest.java:26)

My de-serialization code is as follows :

package mainmenutest;

/**
 *
 * @author Darren Estcourt
 */
import java.io.*;
public class DeserializeDemo
{
   public void Deserialize()
   {
      ClubInfo club = null;
      ClubInfo club2 = null;
      try
      {
         FileInputStream fileIn = new FileInputStream("C:/tmp/club.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         club = (ClubInfo) in.readObject();
         club2 = (ClubInfo) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Club class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("Saved game loaded...");
      System.out.println("Name: " + club.teamName);
      System.out.println("Stadium: " + club.stadium);
      System.out.println("Division: " + club.division);
     // System.out.println("SSN: " + club.SSN);
      System.out.println("Stadium Capacity: " + club.stadiumCapacity);
      System.out.println("Name : " + club2.teamName);

    }
}

I don't want someone to fix it for me, just a pointer in the right direction. I plan to deserialize around 20 objects eventually.

EOFException is thrown when there are no more objects to read, or when the stream unexpectedly reaches its end for some other reason (zero length, it got truncated, it wasn't flushed properly when closed).

In this case the exception was thrown by readObject() , so only the first applies. Looks like you either serialized no objects and are trying to read one, or you serialized one object and are trying to read two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM