Elton.fd
Elton.fd

Reputation: 1595

handling EOFException in java

I have created a method in my java assignment to write into a file from a LinkedList (I used serialization) , then I have created another method to read the file into the inkedList. The following is my method's body:

try {
    FileInputStream fin = new FileInputStream("c:\\Info.ser");
    ObjectInputStream ois = new ObjectInputStream(fin);


    Employee e = (Employee) ois.readObject();
    linkP.add(e);


} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

but it doesnt work right. I think this part:

Employee e = (Employee) ois.readObject();
linkP.add(e);

reads only the first object of the file into the linkedlist and ignores other objects. I surrounded it for loop and while loop several times but it causes EOFException. How can I change my method to read all of the file's objects into the LinkedList?

Upvotes: 0

Views: 1473

Answers (7)

user207421
user207421

Reputation: 310883

Just catch EOFException separately inside your reading loop and process it accordingly, i.e. break out of the loop.

Upvotes: 0

stacker
stacker

Reputation: 68962

If you used LinkedList for serialization you should expect a LinkedList to deserialize:

 linkP= (LinkedList) ois.readObject(); 

instead of

Employee e = (Employee) ois.readObject();   
    linkP.add(e);  

Upvotes: 3

mhaller
mhaller

Reputation: 14232

Note: Please also add the code where you create the info.ser file, since you may have corrupted the ObjectOutputStream by closing/reopening it for each object.

But to answer your question, the proper way of doing it (without catching exceptions) would be:

@Test
public void testSerializingListByEntries() throws Exception {
    List<Serializable> list = new ArrayList<Serializable>();
    list.add(new Date());
    list.add(new Date());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeInt(list.size()); // Magic
    for(Serializable o : list) {
        oos.writeObject(o);
    }
    oos.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    int count = ois.readInt();
    List<Object> newList = new ArrayList<Object>();
    for(int i = 0; i < count;i++) {
        newList.add(ois.readObject());
    }
    ois.close();
    assertEquals(list,newList);
}

Yes, you need to close the streams yourself of course. Omitted for readability.

Upvotes: 1

LeonT
LeonT

Reputation: 56

The easiest way is to include the size of the list as the first thing written to the file. When you read the file, the first thing you retrieve is the size. Then you can read the expected number of objects.

Upvotes: 1

hhafez
hhafez

Reputation: 39750

surround Employee e = (Employee) ois.readObject(); linkP.add(e); with a for loop as you suggested and surround the .readObject call with a try/catc(EOFException)

Upvotes: 0

javamonkey79
javamonkey79

Reputation: 17775

Are you sure that the serialized file contains all of the elements? It looks to me like you might only be serializing one.

Upvotes: 1

Michael
Michael

Reputation: 2634

Would probably need to see how you're writing in the first place but generally:

ObjectInputStream is = null;
try 
{
    is = new ObjectInputStream(new FileInputStream("c:/Info.ser"));
    Object object = null;
    while ((object = is.readObject()) != null) 
    {
        linkP.add(object);
    }
}
catch(Exception e)
{
    //Whatever you need to do
}
finally 
{
    //Never forget to close your streams or you'll run into memory leaks
    try 
    {
        if (is != null) 
        {
            is.close();
        }
    } 
    catch (Exception ex) 
    {
        ex.printStackTrace();
    }
}

Also, its probably better practice for you to handle the exceptions individually but I can't really tell what the streams throw so replace the (Exception e) with everything else.

Upvotes: 0

Related Questions