Tony B
Tony B

Reputation: 344

Serialization of an arraylist which contains another arraylist

my research on google-search and stackoverflow regarding this problem didn't resolve it.

i'd like to show you a snipped of my Datastructure:

there's a class, called "SequenceHolder" => this one carries an:

ArrayList<SequenceData> data;

within the Sequenceholder, there is a function to call the serialization:

public void writeSequenceList() throws FileNotFoundException, IOException {        
    FileOutputStream fout = new FileOutputStream(path);
    ObjectOutputStream oout = new ObjectOutputStream(fout);
    oout.writeObject(data);
    oout.close();
    fout.close();        
}

The class SequenceObject has following fields: (this one is on the top, where i start the serialization)

private ArrayList<SequenceModel> recordedSequenceData;
private String sequenceUrl;

while the SequenceModel is defined like this:

private Object sequenceRawData;    
private boolean isProcessedByRequest;

The sequenceRawdata objects are basically two other classes (containing Strings only)!

every class of this "trail" implements the interface "Serializable".

this is the deserialization:

  public ArrayList<SequenceData> loadSequenceList() throws FileNotFoundException, IOException, ClassNotFoundException {
        FileInputStream fileIn = new FileInputStream(path);
        ObjectInputStream in = new ObjectInputStream(fileIn);
        this.data = (ArrayList<SequenceData>) in.readObject();
        in.close();
        fileIn.close();
        return data; // load from de-serialization
    }

after a deserialization of the SequenceObject, i'll only retrieve the "sequenceUrl", but no recordedSequenceData. Is there a trick to do this?!

It came just up to my mind, to extend some classes with the ObjectOutputStream and call the writingprocess with "this" explicitly in every class - but yeah, i am not sure if thats a good idead.

Upvotes: 0

Views: 188

Answers (1)

codingenious
codingenious

Reputation: 8653

What do you mean by "The sequenceRawdata objects are basically two other classes (containing Strings only)!" because when I try to run the same program :

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;


class SequenceModel implements Serializable
{
    public SequenceModel(Object a, boolean b)
    {
        sequenceRawData = a;
        isProcessedByRequest = b;
    }

    public String toString()
    {
        return (String)sequenceRawData + isProcessedByRequest + " SeqModel ";
    }

    private Object sequenceRawData;    
    private boolean isProcessedByRequest;
}

class SequenceData implements Serializable
{
    public SequenceData(ArrayList<SequenceModel> a, String b)
    {
        recordedSequenceData = a;
        sequenceUrl = b;
    }

    public String toString()
    {
        return recordedSequenceData + sequenceUrl + " SeqData ";
    }

    private ArrayList<SequenceModel> recordedSequenceData;
    private String sequenceUrl;
}

class SequenceHolder implements Serializable
{
    ArrayList<SequenceData> data;

    public String toString()
    {
        return data + " SeqHol ";
    }

    public SequenceHolder(ArrayList<SequenceData> a)
    {
        data = a;
    }


    public void writeSequenceList() throws FileNotFoundException, IOException {        
        FileOutputStream fout = new FileOutputStream(Test.file);
        ObjectOutputStream oout = new ObjectOutputStream(fout);
        oout.writeObject(data);
        oout.close();
        fout.close();        
    }

    public ArrayList<SequenceData> loadSequenceList() throws FileNotFoundException, IOException, ClassNotFoundException {
        FileInputStream fileIn = new FileInputStream(Test.file);
        ObjectInputStream in = new ObjectInputStream(fileIn);
        this.data = (ArrayList<SequenceData>) in.readObject();
        in.close();
        fileIn.close();
        return data; // load from de-serialization
    }
}

public class Test 
{
    public static File file = new File("abc.txt");

    public static void main(String[] args) 
    {

        SequenceModel obj = new SequenceModel("abc", false);
        ArrayList list = new ArrayList(); list.add(obj);
        SequenceData obh = new SequenceData(list, "str");
        ArrayList l2 = new ArrayList();
        l2.add(obh);
        SequenceHolder obi = new SequenceHolder(l2);
        try {
        obi.writeSequenceList();

            System.out.println(obi.loadSequenceList());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

it is able to serialize and deserialize both and there is no problem.

Output it is printing is : [[abcfalse SeqModel ]str SeqData ] which includes everything as desired.

Please let me know if I am missing anything.

Upvotes: 1

Related Questions