简体   繁体   中英

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?

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);  

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

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.

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.

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.

surround Employee e = (Employee) ois.readObject(); linkP.add(e); 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)

只需在阅读循环中单独捕获EOFException并进行相应处理即可,即脱离循环。

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