简体   繁体   English

意外的EOF异常

[英]Unexpected EOF exception

I am trying to read/write an ArrayList of custom (serializable) objects to a file. 我正在尝试将自定义(可序列化)对象的ArrayList写入/写入文件。 It is working as expected, except for one thing: I am getting an EOFexception when reading the file. 它可以按预期工作,除了以下几点:读取文件时出现EOFexception。 Here is my code: 这是我的代码:

class FileHandler {
    private final String FILENAME = "storage.txt";

    ArrayList<Plane> readFromFile(Context context) {
        ArrayList<Plane> returnList = new ArrayList<>();
        Plane temp;

        try {
            FileInputStream fileInputStream = context.openFileInput(FILENAME);
            ObjectInputStream objectInStream = new ObjectInputStream(fileInputStream);


            while ((temp = (Plane)objectInStream.readObject()) != null) {
                returnList.add(temp);
            }

            objectInStream.close();
        }
        catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return returnList;
    }

    void writeToFile(ArrayList<Plane> inData, Context context) {
        FileOutputStream fileOutputStream;
        ObjectOutputStream outputStream;

        try {
            fileOutputStream = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
            outputStream = new ObjectOutputStream(fileOutputStream);

            for (Plane p: inData) {
                outputStream.writeObject(p);
            }

            outputStream.close();
            fileOutputStream.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The file gets written and read as intended, but I'm getting an EOF exception when reading the file. 文件将按预期方式写入和读取,但是读取文件时出现EOF异常。 No idea why. 不知道为什么。 I thought my while-loop would make sure that couldn't happen? 我以为我的while循环可以确保不会发生这种情况?

  1. Why am I getting this exception? 为什么会出现此异常?
  2. Plane is serializable. 飞机是可序列化的。 Is it possible to read and write to file if I changed Plane to be parcable instead? 如果我将Plane更改为可解析,是否可以读写文件? (How?) (怎么样?)

Any attempt to read object data which exceeds the boundaries of the custom data written by the corresponding writeObject method will cause an OptionalDataException to be thrown with an eof field value of true. 任何尝试读取对象数据的尝试都超出了由相应的writeObject方法写入的自定义数据的范围,则将引发eData字段值为true的OptionalDataException。 Non-object reads which exceed the end of the allotted data will reflect the end of data in the same way that they would indicate the end of the stream: bytewise reads will return -1 as the byte read or number of bytes read, and primitive reads will throw EOFExceptions. 超出分配数据末尾的非对象读取将以指示数据流结束的相同方式反映数据的结束:按字节读取将返回-1作为读取的字节或读取的字节数,并且原始读取将引发EOFExceptions。 If there is no corresponding writeObject method, then the end of default serialized data marks the end of the allotted data. 如果没有相应的writeObject方法,则默认序列化数据的结尾将标记分配数据的结尾。

This is what I found on https://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html 这是我在https://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html上找到的

The last attempt to read data occurs when there is no data. 没有数据时,将进行最后一次读取数据的尝试。 The while-loop condition doesn't really check whether there is data to read or not. while循环条件并不真正检查是否有要读取的数据。 It tries to read it regardless. 不管它尝试读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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