Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize multiple Java Objects

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!

like image 839
imalik8088 Avatar asked Apr 22 '13 11:04

imalik8088


2 Answers

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.

like image 85
NilsH Avatar answered Nov 06 '22 04:11

NilsH


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;
    }
like image 28
Tomasz Avatar answered Nov 06 '22 02:11

Tomasz