0

I can't seem to find a way to make readObject() transfer it's contents to an object variable. When I step through the Load function I get to "temp = (HashMap) ois.readObject();" Before this line is executed I am able to see the HashMap's data that I've written with oos in the expressions window of Eclipse so I know the data is there, however when this line executes I'm jumped to the IOException catch with an EOF. From what I've read this is expected, but I have not found a way to catch the EOF (loops with available() and readObjectInt() did not work). I'm running this on an Android emulator. Any suggestions would be appreciated.

public void Save(Pottylog data)
{
    try 
    {
        FileOutputStream fos = openFileOutput("Plog", Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(data.get());
        oos.close();
    }
    catch (FileNotFoundException ex)
    {
        ex.printStackTrace();
    }
    catch (java.io.IOException e)
    {
        e.printStackTrace();
    }
}

public HashMap<String, Integer> Load()
{
    HashMap<String, Integer> temp = null;

    try 
    {
        FileInputStream fis = openFileInput("Plog");
        ObjectInputStream ois = new ObjectInputStream(fis);
        temp = (HashMap<String, Integer>) ois.readObject();

    } 
    catch (FileNotFoundException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (ClassNotFoundException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

    return temp;
}
Rajendra_Prasad
  • 1,300
  • 4
  • 18
  • 36

1 Answers1

0

EOFException means you have reached the end of the stream. I don't know what you think you're seeing in the debugger, but there is no object there in the stream to be read. catch(EOFException exc) does work; at this point you should close the stream and exit the reading loop. Don't misuse available() as an end of stream test: that's not what it's for.

user207421
  • 305,947
  • 44
  • 307
  • 483