Java中ArrayList的序列化
我试图序列化ArrayList,但当我反序列化时,返回了所有空值。不明白为什么Java中ArrayList的序列化,java,Java,我试图序列化ArrayList,但当我反序列化时,返回了所有空值。不明白为什么 public ArrayList<Uurwerken> maakUurwerk() { lijst.add(new PolsAnaloog(time, "roze", false, "PA01")); lijst.add(new PolsDigitaalTimer(time, "zwart", true, "PDT01")); lijst.add(new PolsAnaloog(t
public ArrayList<Uurwerken> maakUurwerk() {
lijst.add(new PolsAnaloog(time, "roze", false, "PA01"));
lijst.add(new PolsDigitaalTimer(time, "zwart", true, "PDT01"));
lijst.add(new PolsAnaloog(time, "rood", false, "PA02"));
lijst.add(new PolsAnaloogDatum(time, "zwart", true, "PAD01"));
lijst.add(new PolsDigitaalDatumTimer(time, "groen", true, "PADT01"));
lijst.add(new Wandklok(time, 10, 20, "geel", "Wand01"));
lijst.add(new Wandklok(time, 50, 30, "wit", "Wand02"));
lijst.add(new Wandklok(time, 5, 15, "blauw", "Wand03"));
lijst.add(new Wandklok(time, 60, 60, "bruin", "Wand04"));
lijst.add(new Wandklok(time, 80, 100, "wit", "Wand05"));
return lijst;}
public ArrayList maakUurwerk(){
添加(新Polsanalog(时间,“roze”,假,“PA01”);
添加(新的PolsDigitaalTimer(时间,“zwart”,真,“PDT01”);
添加(新的Polsanalog(时间,“rood”,假,“PA02”);
添加(新的Polsanalogdatam(时间,“zwart”,真,“PAD01”);
添加(新的PolsDigitaalDatumTimer(时间,“groen”,true,“PADT01”);
添加(新的Wandklok(时间,10,20,“geel”,“Wand01”);
添加(新的Wandklok(时间,50,30,“wit”,“Wand02”);
添加(新的Wandklok(时间,5,15,“blauw”,“Wand03”);
添加(新的Wandklok(时间,60,60,“布鲁因”,“Wand04”);
添加(新的Wandklok(时间,80100,“wit”,“Wand05”);
返回lijst;}
要执行序列化,我有以下代码:
public SerialOutput() {
// TODO Auto-generated constructor stub
}
public void schrijfLijstWeg(ArrayList<Uurwerken> klok) {
FileOutputStream bestand = null;
ObjectOutputStream out = null;
try {
bestand = new FileOutputStream(Dir.PAD_Serial);
out = new ObjectOutputStream(bestand);
out.writeObject(klok);
} catch (IOException ex) {
System.out.println("Er is iets verkeerds gelopen bij het wegschrijven");
System.out.println(ex.getMessage());
}
finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public ArrayList<Uurwerken> leesLijst() {
FileInputStream bestand = null;
ObjectInputStream in = null;
ArrayList<Uurwerken> klokLijst = new ArrayList<Uurwerken>();
try {
bestand = new FileInputStream(Dir.PAD_Serial);
in = new ObjectInputStream(bestand);
klokLijst = (ArrayList<Uurwerken>) in.readObject();
}
catch (Exception ex) {
System.out.println("Er is iets verkeerd gelopen met het inlezen");
System.out.println(ex.getMessage());
ex.printStackTrace();
}
finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return klokLijst;
}
public void toonUurwerk(ArrayList<Uurwerken> uurwerk) {
Iterator<Uurwerken> it = uurwerk.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
publicserialoutput(){
//TODO自动生成的构造函数存根
}
公共空间schrijfLijstWeg(ArrayList klok){
FileOutputStream bestand=null;
ObjectOutputStream out=null;
试一试{
bestand=新文件输出流(Dir.PAD\u串行);
out=新的ObjectOutputStream(bestand);
out.writeObject(klok);
}捕获(IOEX异常){
System.out.println(“Er是iets Verkeers gelopen bij het wegschrijven”);
System.out.println(例如getMessage());
}
最后{
试一试{
out.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
公共阵列列表leesLijst(){
FileInputStream bestand=null;
ObjectInputStream in=null;
ArrayList klokLijst=新的ArrayList();
试一试{
bestand=新文件输入流(Dir.PAD\u串行);
in=新的ObjectInputStream(bestand);
klokLijst=(ArrayList)in.readObject();
}
捕获(例外情况除外){
System.out.println(“Er是iets verkeerd gelopen遇到了het inlezen”);
System.out.println(例如getMessage());
例如printStackTrace();
}
最后{
试一试{
in.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回klokLijst;
}
公共无效Tounuurwerk(ArrayList uurwerk){
迭代器it=uurwerk.Iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
Main包含以下代码
public static void main(String[]args){
Fabriek f = new Fabriek();
Serializatie so = new Serializatie();
ArrayList <Uurwerken> list = new ArrayList<Uurwerken>();
list.addAll(f.maakUurwerk());
so.schrijfLijstWeg(list);
ArrayList<Uurwerken> temp = so.leesLijst();
so.toonUurwerk(temp);
}
publicstaticvoidmain(字符串[]args){
Fabriek f=新的Fabriek();
Serializatie so=new Serializatie();
ArrayList=新建ArrayList();
list.addAll(f.maakUurwerk());
so.schrijfLijstWeg(列表);
ArrayList temp=so.leesLijst();
Tounuurwerk(临时);
}
我试着运行so.toonUurwerk(列表),然后我得到了正确的值。所以我按它确实序列化ok;但是在反序列化中出错了。如前所述,当我运行so.toonUurwerk时,它返回所有“null”值 我很确定你的问题在这里:
list.addAll(f.maakUurwerk());
以及:
public ArrayList maakUurwerk(){
添加(新Polsanalog(时间,“roze”,假,“PA01”);
AsmaakUurwerk()
没有创建它返回的列表。此列表是全局变量吗?是否已创建/实例化
如果不是,这个方法可能返回null,这意味着你正在序列化null,这就是为什么它会打印出null!,你的问题是…?序列化的结果是什么,你能进一步解释一下“股票地址”吗p、 如果它不在dutchy中,这将更容易理解。您也可以在序列化之前调用
so.toonUurwerk(f.maakUurwerk());
,以检查f.maakUurwerk()
是否真的是您认为的“股票地址”?您的类是否覆盖Uurwerk
toString?如果不是,它将显示为类似Uurwerk@4efc3a
。如果您的意思是我尝试过将列表置为静态,那么是的。列表以及我尝试置为静态的方法,但没有更改。列表在Fabriek()的de构造函数中初始化。还有其他建议吗?
public ArrayList<Uurwerken> maakUurwerk() {
lijst.add(new PolsAnaloog(time, "roze", false, "PA01"));