简体   繁体   English

如何在Java中使用ObjectInputStream从文件中读取对象列表?

[英]How to read a List of List of Objects from a file using ObjectInputStream in Java?

I have a List of list of some objects defined in a data structure: List > someObject1=new ArrayList(); 我有一个在数据结构中定义的一些对象的列表列表:List> someObject1 = new ArrayList();

And I have written few someObject items to a fileXY using function writeData : 而且我已经使用函数writeData将一些someObject项目写入了fileXY:

void writeData(List <List<Integer>> someObject1, File XY) throws IOException {
    try(ObjectOutputStream outFile = new ObjectOutputStream(new FileOutputStream(XY,true)))
    outFile.writeObject(someObject1);
    }
    catch( IOException ex) { ex.printStackTrace(); } 
}

Now I tried to read it using function readData: 现在,我尝试使用readData函数读取它:

void readData(File XY) throws IOException {
    try (ObjectInputStream inFile=new ObjectInputStream(new FileInputStream(XY))) {
    List <List <MyClass>> someObject2=(List <List <MyClass>>)inFile.readObject();
    }
catch( IOException ex) { ex.printStackTrace(); } 
}

But it is giving me error : 但这给了我错误:

java.io.OptionalDataException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1363)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)

Can anyone help me with it. 谁能帮助我。 Is there something wrong with the casting while reading this data structure? 读取此数据结构时,转换是否有问题?

Had it been just a list of objects, I could have read it by casting the readObject() with 如果只是对象列表,我可以通过将readObject()与

(List<MyClass>).

Why the same thing doesn't work for the list-of-list of objects? 为什么同一件事对对象列表不起作用?

This is outline of MyClass: 这是MyClass的概述:

public class MyClass implements Serializable {
private long aa;    
private List <Integer> bb;
public MyClass(long a,List <Integer> b) {
    aa=a;
    bb=b;         
}
public long getA() {    
    return aa;
}         

public List<Integer> getB () {
   return bb; 
} 

} }

This definitely works for me: 这绝对适合我:

public static final String FILE = "file.b";

public static class MyClass implements Serializable {
    private long aa;
    private List<Integer> bb;

    public MyClass(long a, List<Integer> b) {
        aa = a;
        bb = b;
    }
}

public static void main(String[] args) throws FileNotFoundException, IOException,
        ClassNotFoundException {
    List<List<MyClass>> list = new ArrayList<>();
    list.add(new ArrayList<>());
    list.get(0).add(new MyClass(5, new ArrayList<>()));

    ObjectOutputStream outFile = new ObjectOutputStream(new FileOutputStream(new File(FILE)));
    outFile.writeObject(list);
    outFile.close();

    ObjectInputStream inFile = new ObjectInputStream(new FileInputStream(new File(FILE)));
    List<List<MyClass>> list2 = (List<List<MyClass>>) inFile.readObject();
    System.out.println(list2);
}

Note: this works even if I don't add any MyClass to list.get(0) , and even if I don't add a list to list . 注意:即使我没有将任何 MyClass 添加 list.get(0) ,即使我没有将列表添加到 list

Several possible causes for your problem come to my mind: 我想到您的问题的几种可能原因:

  • you forgot to close the output stream after writing to it 您忘记在写入后关闭输出流
  • you don't read the object at the right position in the file. 您不会在文件中的正确位置读取对象。 If you have several objects written to the file, they must be read in the same order as they were written. 如果您有多个对象写入文件,则必须以与写入相同的顺序读取它们。
  • your problem is with your custom class 您的问题出在您的自定义课程上

UPDATE: 更新:

Maybe you use a non serializable attribute in your serializable class MyClass . 也许您在可序列化的类MyClass使用了不可序列化的属性。 If so, you should override writeObject() and readObject() to serialize/deserialize such an attribute of your class properly. 如果是这样,则应重写writeObject()readObject()以正确地序列化/反序列化此类的属性。

Take a look at this: http://marxsoftware.blogspot.fr/2014/02/serializing-java-objects-with-non.html 看看这个: http : //marxsoftware.blogspot.fr/2014/02/serializing-java-objects-with-non.html

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

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