Hi!
I'm trying to save/load all my program's data with just one big serialization.(I'm really depending on this)
Below are the codes I used to test serialization---but it seems that something is wrong with it. I'm too confused, since I learnt serialization as a 'magical genie' kind of thing that saves/loads any objects without any fuss.
public class main {
public static void main(String[] args){
_global gb = new _global();
temp_class b1 = new temp_class();
b1.abc.add(2);
b1.abc.add(4);
temp_class b2 = new temp_class();
b2.abc.add(3);
b2.abc.add(5);
gb.save_c.add(b1);
gb.save_c.add(b1);
gb.save_c.add(b2);
gb.save_c.add(b1);
temp_class2 b3 = new temp_class2();
b3.tc.add(b1);
b3.tc.add(b2);
b3.tc.add(b1);
gb.SaveGlobalOnFile("C:\\test.txt");
gb.LoadGlobalFromFile("C:\\test.txt");
if(gb.get_c.get(0) == b1){
System.out.print("b1");
}
else if(gb.get_c.get(0) == b2){
System.out.print("b2");
}
else{
System.out.print("?!!");
}
}
}
public class _global implements Serializable {
private static final long serialVersionUID = 1L;
ArrayList<temp_class> get_c = new ArrayList<temp_class>(0);
ArrayList<temp_class> save_c = new ArrayList<temp_class>(0);
ArrayList<temp_class2> get_d = new ArrayList<temp_class2>(0);
ArrayList<temp_class2> save_d = new ArrayList<temp_class2>(0);
public void LoadGlobalFromFile(String filename){
_global gb = new _global();
try{
FileInputStream fileIn = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fileIn);
get_c = (ArrayList<temp_class>) in.readObject();
//get_d = (ArrayList<temp_class2>) in.readObject();
in.close();
fileIn.close();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
void SaveGlobalOnFile(String filename){
try {
System.out.println("Creating File/Object output stream...");
FileOutputStream fileOut = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
System.out.println("Writing Hashtable Object...");
out.writeObject(save_c);
//out.writeObject(save_d);
System.out.println("Closing all output streams...\n");
out.close();
fileOut.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class temp_class implements Serializable{
private static final long serialVersionUID = 3419136556899602822L;
ArrayList<Integer> abc = new ArrayList<Integer>(0);
}
public class temp_class2 implements Serializable{
private static final long serialVersionUID = 7400063825951715672L;
ArrayList<temp_class> tc = new ArrayList<temp_class>(0);
}
I was expecting "b1" or "b2" as the output of the main file, but the test-application only returns "?!!"
Thank you!