我想序列化一个数组列表Item但它不起作用......
my Item类扩展Stuff类并有一些子类.
我的所有类都实现了可序列化。
我有这部分:
try{
// Serialize data object to a file
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("MyData.ser"));
out.writeObject(myData);
out.close();
// Serialize data object to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(myData);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
} catch (IOException e) {
}
我的课程 :
public class Stuff implements Serializeable{
....
some protected fields
.
.
}
public class Item extends Stuff implements Serializable{
...
..
.
} and some subclasses of Item:
public class FirstItem extends Item implements Serializable{
...
}
public class SecondItem extends Item implements Serializable{
...
} ... I want to serialize an object contains ArrayList of <Item> that has objects of Item's subclasses (FirstItem,SecondItem,...)
我认为信息已经足够了...
这只是一个小错误,现在可以正常工作了......
我为我的愚蠢问题感到抱歉。
谢谢您的回答。
您可以像这样序列化 ArrayList 的类
public class MyData implements Serializable {
private long id;
private String title;
private ArrayList<String> tags;
...
public String getTitle() {
}
}
并创建可序列化的
ArrayList<MyData> myData = new ArrayList<MyData>();
try{
// Serialize data object to a file
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("MyData.ser"));
out.writeObject(myData);
out.close();
// Serialize data object to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(myData);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
} catch (IOException e) {
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)