Java将对象输入流读取到数组列表中?

发布于 2024-11-17 17:15:34 字数 781 浏览 5 评论 0原文

下面的方法应该将二进制文件读入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)

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

The method below is supposed to read a binary file into an arrayList. But getting a 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长梦不多时 2024-11-24 17:15:34

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

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

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

catch (EOFException exc)
{
 in.close();
 break;
}
江南月 2024-11-24 17:15:34

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

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

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

MediaLibrary obj = null;
while ((obj = (MediaLibrary)objIn.readObject()) != null) {
     libraryFromDisk.add(obj);
}
聆听风音 2024-11-24 17:15:34

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

while (objIn.readObject() != null)

然后您正在读取以下对象:

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

因此,在一次迭代中您应该只读取一个对象

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

You're reading an object in the while test:

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();
    }
}
迷你仙 2024-11-24 17:15:34

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

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

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();
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文