java.io.StreamCorruptedException:无效的类型代码:AC ...反序列化时

时间:2017-08-24 07:58:08

标签: java serialization

所以我试图序列化/反序列化LinkedListFeed

@Test
public void writeAndReadTest() throws Exception {

    Feed feed = new Feed();
    feed.setSubject("testsub");
    feed.setLink("bla");
    feed.setDescription("basdfasdfasdf");
    feed.setDate("DATUM");
    feed.setId(234);
    feed.setAsXML("XMLXMLXML");

    Feed feed2 = new Feed();
    feed2.setSubject("222testsub");
    feed2.setLink("222bla");
    feed2.setDescription("222basdfasdfasdf");
    feed2.setDate("222DATUM");
    feed2.setId(2346);
    feed2.setAsXML("222XMLXMLXML");

    LinkedList<Feed> list = new LinkedList<>();
    list.add(feed);
    list.add(feed2);

    list.forEach(System.out::println);

    Application.writeFeedsIntoFile(list,"C:\\\\Users\\JARE\\Desktop\\anderesZeug\\feedLog.txt");
    System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

    LinkedList<Feed> listlist = Application.readFeedsFromFile("C:\\\\Users\\JARE\\Desktop\\anderesZeug\\feedLog.txt");
    System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    listlist.forEach(System.out::println);
}

这是抛出异常的方法:

  /**
     * Reads the feeds which have been sent from a file, parses them into Feeds
     *
     * @param filePath ... path to the storage file
     * @throws IOException ... e.g. file not found or corrupted or whatever
     */
    public static LinkedList<Feed> readFeedsFromFile(String filePath) throws IOException {
        LinkedList<Feed> res = new LinkedList<>();

        ObjectInputStream oiS = null;
        try {
            FileInputStream streamIn = new FileInputStream(filePath);
            oiS = new ObjectInputStream(streamIn);
            LinkedList<Feed> readCase;

            do {

                readCase = (LinkedList<Feed>) oiS.readObject();  //TODO Here its thrown
                if (readCase != null) {
                    res.addAll(readCase);
                }

            } while (readCase != null);

        } catch (  StreamCorruptedException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (oiS != null) {
                oiS.close();
            }
        }
        return res;

    }

我得到的是输出:

Feed{date='DATUM', subject='testsub', link='bla', description='basdfasdfasdf', id=234}
Feed{date='222DATUM', subject='222testsub', link='222bla', description='222basdfasdfasdf', id=2346}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                java.io.StreamCorruptedException: invalid type code: AC
                    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1379)
                    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
                    at main.java.Application.readFeedsFromFile(Application.java:93)
                    at test.java.ApplicationTest.writeFeedsIntoFile(ApplicationTest.java:47)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                    at java.lang.reflect.Method.invoke(Method.java:498)
                    at 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Feed{date='DATUM', subject='testsub', link='bla', description='basdfasdfasdf', id=234}
Feed{date='222DATUM', subject='222testsub', link='222bla', description='222basdfasdfasdf', id=2346}

Process finished with exit code 0

我被困在这里。我读了关于StreamCorruptedException的类似问题/答案,但无法弄清楚如何在我的代码中解决这个问题。

IDE提醒我:enter image description here ......这对我也没有帮助。

就我而言,我得到了正确的序列化/反序列化对象 - 但让它运行并定期抛出异常并不是真的可以接受。请帮忙!

0 个答案:

没有答案