Java将对象输入流读入arraylist?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6484428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:19:10  来源:igfitidea点击:

Java read object input stream into arraylist?

javafileinputstream

提问by ash

The method below is supposed to read a binary file into an arrayList. But getting a java.io.EOFException:

下面的方法应该将二进制文件读入arrayList. 但是得到一个java.io.EOFException

at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2553)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1296)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at .... Read(Tester.java:400) 
at .... main(Tester.java:23)

Line 23 at main just calls the method, line 400 is the while loop below. Any ideas?

main 的第 23 行只是调用了该方法,第 400 行是下面的 while 循环。有任何想法吗?

private static void Read() {
    try {
        ObjectInputStream objIn = new ObjectInputStream(new FileInputStream("/file.bin"));
        while (objIn.readObject() != null) {
            list.add((Libreria) objIn.readObject());
        }
        objIn.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

回答by Vincent Ramdhanie

The problem is that you are calling readObject() twice in the loop. Try this instead:

问题是您在循环中两次调用 readObject() 。试试这个:

MediaLibrary obj = null;
while ((obj = (MediaLibrary)objIn.readObject()) != null) {
     libraryFromDisk.add(obj);
}

回答by Jihed Amine

You're reading an object in the while test:

您正在 while 测试中读取一个对象:

while (objIn.readObject() != null)

Then you're reading the next object in:

然后你正在阅读下一个对象:

libraryFromDisk.add((MediaLibrary) objIn.readObject());

So in one iteration you should read only one object

所以在一次迭代中你应该只读取一个对象

private static void Load() {
    try {
        ObjectInputStream objIn = new ObjectInputStream(new FileInputStream("/file.bin"));
        Object object = objIn.readObject();
        while (object != null) {
           libraryFromDisk.add((MediaLibrary) object);
           object = objIn.readObject();

        }
        objIn.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

回答by user207421

As per the other answers you are reading twice in the loop. Your other problem is the null test. readObject()only returns null if you have written a null, not at EOS, so there's not much point in using it as a loop termination test. The correct termination of a readObject()loop is

根据其他答案,您在循环中阅读了两次。您的另一个问题是空测试。readObject()如果您写了空值,则仅返回空值,而不是在 EOS 上,因此将其用作循环终止测试没有多大意义。readObject()循环的正确终止是

catch (EOFException exc)
{
 in.close();
 break;
}

回答by duyetpt

You can try this. Good luck!

你可以试试这个。祝你好运!

private static void Load() {
try {
    ObjectInputStream objIn = new ObjectInputStream(new FileInputStream("/file.bin"));
    boolean check=true;
    while (check) {

try{
object = objIn.readObject();
libraryFromDisk.add((MediaLibrary) object);
}catch(EOFException ex){
check=false;
}

    }
    objIn.close();
} catch(Exception e) {
    e.printStackTrace();
}
}