0

我对 google-search 和 stackoverflow 关于这个问题的研究并没有解决它。

我想向您展示我的数据结构的片段:

有一个类,叫做“SequenceHolder”=> 这个有一个:

ArrayList<SequenceData> data;

在Sequenceholder中,有一个调用序列化的函数:

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

SequenceObject 类具有以下字段:(这个在顶部,我开始序列化的地方)

private ArrayList<SequenceModel> recordedSequenceData;
private String sequenceUrl;

而 SequenceModel 的定义如下:

private Object sequenceRawData;    
private boolean isProcessedByRequest;

sequenceRawdata 对象基本上是另外两个类(仅包含字符串)!

这个“trail”的每个类都实现了“Serializable”接口。

这是反序列化:

  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
    }

在对 SequenceObject 进行反序列化后,我将只检索“sequenceUrl”,但没有记录的SequenceData。有没有什么技巧可以做到这一点?!

我想到了,用 ObjectOutputStream 扩展一些类,并在每个类中明确地用“this”调用写作过程——但是,我不确定这是否是个好主意。

4

1 回答 1

1

“sequenceRawdata 对象基本上是另外两个类(仅包含字符串)”是什么意思因为当我尝试运行相同的程序时:

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();
        }
    }
}

它能够序列化和反序列化两者,没有问题。

它正在打印的输出是:[[abcfalse SeqModel ]str SeqData ],其中包括所需的所有内容。

如果我遗漏了什么,请告诉我。

于 2013-10-17T15:25:04.767 回答