0

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.

1
  • What you describe for ObjectOutputStream is custom serialization using Externalizable. It is quite powerful as it is explicit serialization: you put and get what you want. Commented Oct 17, 2013 at 15:32

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried by assigning a class with two strings to Object sequenceRawData, that worked too!!
hi Batty, first thing which catches my attention is, that you have in every class a "toString()"-Method - i'll try that and i'll give feedback about it. I don't see anything missing - it looks exactly the same, except for the mentioned toString()-Methods. And with "The sequenceRawdata objects are basically two other classes (containing Strings only)!" was behind, that those sequenceRawData Objects are just two other classes, which contains only strings. (well as you figured yourself out, just to be clear about this)
toString() doesn't play any part in serialization, its just to print object values in representable manner, otherwise objects will be printed as SequenceModel@3213a2 something, so use toString only if you print the object, otherwise no need of it :)
okay... well still doesnt work for me :( I've noticed, that there's a Constructor with params to set the values -> is that the case? I only have setter and getter as function, but not in the constructor.... okay: now i am getting a bit mad. i added all getter and setter method, even of the missing fields, then i added constructors with full setter of all variables. but it isnt still working.... the problem is, that the arraylist of the sequenceData won't be serialized :/ maybe i should post the code? hmmm
curiosly i checked the data, which is going to be saved to disk. and this data is exact the data which is going to be serialized. i just saw, that i didn't fill the recordedSequenceData ...... now it works :) THanks man, you helped me ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.