我尝试使用Externalizable
接口在Java中序列化大尺寸对象。
public class ResultsData implements Externalizable{
private static final long serialVersionUID = 1L;
private ArrayList<ALotOfResults1> results1;
private ArrayList<ALotOfResults2> results2;
private ArrayList<ALotOfResults3> results3;
private int selection;
public ResultsData(){
results1=new ArrayList<ALotOfResults1>();
results2=new ArrayList<ALotOfResults2>();
results3=new ArrayList<ALotOfResults3>();
}
//Getter and setter omited
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(results1);
out.writeObject(results2);
out.writeObject(results3);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
switch(selection) {
case 0:
results1 = (ArrayList)in.readObject();
break;
case 1:
in.readObject();
results2 = (ArrayList)in.readObject();
break;
case 2:
in.readObject();
in.readObject();
results3 = (ArrayList)in.readObject();
break;
}
}
在程序执行期间填充的那三个数组列表具有非常大的大小(每个14 MB)。
public class ResultsManagement {
public static ResultsData loadResultsData(String path,ResultsData resultsData) {
try {
FileInputStream fisProd = new FileInputStream(path+".res");
ObjectInputStream oisProd = new ObjectInputStream(fisProd);
resultsData.readExternal(oisProd);
fisProd.close();
} catch (IOException ioe) {
System.out.println("Error de IO: " + ioe.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println("Error de clase no encontrada: " + cnfe.getMessage());
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
return resultsData;
}
public static void saveResultsData(ResultsData resultsData,String path) {
try {
FileOutputStream fosProd = new FileOutputStream(path+".res");
ObjectOutputStream oosProd = new ObjectOutputStream(fosProd);
resultsData.writeExternal(oosProd);
fosProd.close();
} catch (IOException ioe) {
System.out.println("Error de IO: " + ioe.getMessage());
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
牢不可破的条件是我只想拥有一个文件,例如Documents/Project/project1.res
如何在不加载其他部分的情况下加载对象的某些部分?可能吗? 例如,当我只需要加载第三个arrayList(results1和results2)时,我不需要加载两个第一个arrayList(results1和results2),但我知道访问results3的唯一方法是阅读结果1和结果2.
在ResultsData类中:
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}
public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
return is.readObject();
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
byte[] r1 = serialize(results1);
System.out.println("Bytes in r1: "+r1.length);//42392 Bytes
out.write(r1);
byte[] r2 = serialize(results2);
System.out.println("Bytes in r2: "+r2.length);//19268558 Bytes (a lot of results here)
out.write(r2);
out.close();
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
switch(selection) {
case 0:
byte[] arrayBytes=new byte[42392];
in.read(arrayBytes);
results1 = (ArrayList)deserialize(arrayBytes);
break;
case 1:
in.skipBytes(42392);
byte[] arrayBytes2=new byte[19268558];
in.read(arrayBytes2);
results2 = (ArrayList)deserialize(arrayBytes2);
break;
}
}
答案 0 :(得分:0)
作为通用解决方案,在插入数据时,插入每个列表的大小(以字节为单位)。然后在阅读时,读取大小并跳过你想要跳过的每个列表的那么多字节。根据您的使用案例以及如何在列表中插入/访问数据,可以更加优化解决方案。