gpt4 book ai didi

java - 包含另一个数组列表的数组列表的序列化

转载 作者:行者123 更新时间:2023-12-01 13:59:16 26 4
gpt4 key购买 nike

我对 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 对象基本上是另外两个类(仅包含字符串)!

这个“轨迹”的每个类都实现了“Serialized”接口(interface)。

这是反序列化:

  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”调用写入过程 - 但是,是的,我不确定这是否是一个好主意。

最佳答案

“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],其中包括所需的所有内容。

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

关于java - 包含另一个数组列表的数组列表的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19429727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com