[英]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: 我想到您的问题的几种可能原因:
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.