Deserialize multiple Java Objects

前端 未结 3 437
感动是毒
感动是毒 2020-12-10 15:59

hello dear colleagues,

I have a Garden class in which I serialize and deserialize multiple Plant class objects. The serializing is working but the deserializing is n

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 16:05

    How about serializing the entire list instead? There's no need to serialize each individual object in a list.

    public void searilizePlant(ArrayList _plants) {
        try {
            FileOutputStream fileOut = new FileOutputStream(fileName);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(_plants);
            out.close();
            fileOut.close();
        } catch (IOException ex) {
        }
    }
    
    public List deserializePlant() {
        List plants = null;
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
            plants = in.readObject(); 
            in.close();
        }
        catch(Exception e) {}
        return plants;
    }
    

    If that does not solve your problem, please post more details about your error.

提交回复
热议问题