Currently I have a program that can save and load ArrayLists of type "Variable" from a file. However, I would like to be able to save and load different objects(instances of different classes such as "Variables", "Functions"..etc) from just one file. Here is the code I have now:
public SaveState(){
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream("vars.stt"));
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<Variables> vars = new ArrayList<>();
vars.add(new Variables("g", 5));
vars.add(new Variables("f", -3.12));
try {
out.writeObject(vars);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public LoadState(){
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream("vars.stt"));
} catch (IOException e) {
e.printStackTrace();
}
try {
//final Object o = in.readObject();
final ArrayList<Variables> vars = (ArrayList<Variables>) in.readObject();
System.out.println(vars.toString());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
This code only allows me to load one object type from a file. Is there any way to save/load multiple object types in this manner?