简体   繁体   中英

Java Mysterious EOF exception with readObject

The following code produces an EOFException . Why is that?

public static Info readInfoDataFromFile(Context context) {
    Info InfoData = null;
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    Object object = null;

    if (context.getFileStreamPath("InfoFile.dat").exists()) {
        try {
            fis = context.openFileInput("InfoFile.dat");
            ois = new ObjectInputStream(fis);
            Object temp;
            try {
                // here it throws EOF exception in while loop 
                while ((temp = ois.readObject()) != null) {
                    object = temp;
                }
            } catch (NullPointerException npe) {
                npe.printStackTrace();
            } catch (EOFException eof) {
                eof.printStackTrace();
            } catch (FileNotFoundException fnfe) {
                fnfe.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

StackTrace:

03-07 14:29:01.996: WARN/System.err(13984): java.io.EOFException
03-07 14:29:01.996: WARN/System.err(13984):     at java.io.DataInputStream.readByte(DataInputStream.java:131)
03-07 14:29:01.996: WARN/System.err(13984):     at java.io.ObjectInputStream.nextTC(ObjectInputStream.java:628)
03-07 14:29:01.996: WARN/System.err(13984):     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:907)
03-07 14:29:01.996: WARN/System.err(13984):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262)03-07 14:29:01.996: WARN/System.err(13984):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217)
03-07 14:29:01.996: WARN/System.err(13984):     at 

Depends on how many objects your file contains. If it has only one object, you can deserialise in one step.

try {
    Object temp = ois.readObject();
}
catch(Exception e) {
    //handle it
}

First of all, readObject() only returns null if you wrote null to the stream when creating it. If there is no more data in the stream, it will throw an EOFException .

If you don't expect the EOF, the reason is probably that the stream is corrupt. This can happen if you forget to close it after writing data to it.

I had the same mysterious EOFException and it was only the path of the Object Class to send across the ObjectOutputStream to the ObjectInputStream . They must have the same path (same package name and, of course, same class name).

The definition of readObject() on ObjectInputStream doesn't specify that it will return null when the end of stream is reached. Instead an exception is thrown if you attempt to read an additional object beyond the end of the file.

I received this error because using OutputStream.write() to write an int and InputStream.readInt() to read one.

However, OutputStream.write writes a byte according to the documentation (put accepts an int as parameter), so instead, I needed to use OutputStream.writeInt .

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