我对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对象基本上是另外两个类(仅包含字符串)!
每一类&#34;追踪&#34;实现界面&#34; Serializable&#34;。
这是反序列化:
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
}
在序列化对象的反序列化之后,我只检索&#34; sequenceUrl&#34;,但没有记录的序列数据。 有没有办法做到这一点?!
我想到的是,使用ObjectOutputStream扩展一些类并使用&#34来调用writeprocess;这个&#34;明确地在每个班级 - 但是,我不确定这是否是一个好的理想。
答案 0 :(得分: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] ,其中包含所需的所有内容。
如果我遗漏了任何东西,请告诉我。