Deserialize multiple Java Objects

26,356

Solution 1

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

public void searilizePlant(ArrayList<Plant> _plants) {
    try {
        FileOutputStream fileOut = new FileOutputStream(fileName);
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(_plants);
        out.close();
        fileOut.close();
    } catch (IOException ex) {
    }
}

public List<Plant> deserializePlant() {
    List<Plants> 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.

Solution 2

It may not always be feasible to deserialize a whole list of objects (e.g., due to memory issues). In that case try:

    ObjectInputStream in = new ObjectInputStream(new FileInputStream(
            filename));

    while (true) {
        try {
            MyObject o = (MyObject) in.readObject();
            // Do something with the object
        } catch (EOFException e) {
            break;
        }
    }

    in.close();

Or using the Java SE 7 try-with-resources statement:

    try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(
            filename))) {
        while (true) {
            MyObject o = (MyObject) in.readObject();
            // Do something with the object
        }
    } catch (EOFException e) {
        return;
    }
Share:
26,356
imalik8088
Author by

imalik8088

Updated on July 09, 2022

Comments

  • imalik8088
    imalik8088 almost 2 years

    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 not working if a want to assign it to calling variable in the mein static method.

    public void searilizePlant(ArrayList<Plant> _plants) {
        try {
            FileOutputStream fileOut = new FileOutputStream(fileName);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            for (int i = 0; i < _plants.size(); i++) {
                out.writeObject(_plants.get(i));
            }
            out.close();
            fileOut.close();
        } catch (IOException ex) {
        }
    }
    

    deserializing code:

    public ArrayList<Plant> desearilizePlant() {
        ArrayList<Plant> plants = new ArrayList<Plant>();
        Plant _plant = null;
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
            Object object = in.readObject();
    
           // _plant = (Plant) object;
    
    
            // TODO: ITERATE OVER THE WHOLE STREAM
            while (object != null) {
                plants.add((Plant) object);
                object = in.readObject();
            }
    
            in.close();
        } catch (IOException i) {
            return null;
        } catch (ClassNotFoundException c) {
            System.out.println("Employee class not found");
            return null;
        }
        return plants;
    }
    

    My invoking code:

    ArrayList<Plant> plants = new ArrayList<Plant>();
    plants.add(plant1);
    Garden garden = new Garden();
    garden.searilizePlant(plants);
    
    // THIS IS THE PROBLEM HERE
    ArrayList<Plant> dp = new ArrayList<Plant>();
    dp = garden.desearilizePlant();
    

    edit
    I got a null Pointer exception
    The solution of @NilsH is working fine, thanks!

  • imalik8088
    imalik8088 about 11 years
    @NilsH thank very much it working change: public List<Plant> deserializePlant(){....}
  • faizal
    faizal almost 10 years
    This did not work for me. I get a Stream corruption error. See stackoverflow.com/questions/23889486/…
  • Pankaj Sharma
    Pankaj Sharma almost 10 years
    @faizal Need to modify how the outputstream is created. See stackoverflow.com/questions/1194656/…
  • throws_exceptions_at_you
    throws_exceptions_at_you over 9 years
    Its because of your while condition. You will read from the stream despite it is consumed already.