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!

== compares object references or stands for reference equality which would never hold true for objects which are loaded from a serialized file after being saved. The serialization mechanism uses the serialized byte stream to create a *new* object having the same state as the original saved object. Override the equals method of the Object class for your custom classes if you need logical equality.

So...are the data in the object intact(even when the == sign does not work), or are they damaged?

(Thanks,by the way)

No, the data is not "damaged" if the serialization is successful. You can use a debugger to confirm the same (inspect the fields of your de-serialized object). Also, it's not that the == "does not work", it's just that it has a difference purpose than the one which you seek.

Your deserialised object(s) will contain data with the same values as the original objects - but they will not be the actual same data items as were serialised (eg: you may serialise to a file, quit your app, close the JVM, start a new app in a new JVM and deserialise the objects - values will be the same, but the original objects will be long gone).
== tests for two references being references to exactly thwe same object in the JVM's memory, which these are not.
If it's appropriate in your app (eg) for two of your object to be considered equal if they contain the same values, then you need to give them an equals(Object 0) method that compares all the values and returns true or false as appropriate. This is exactly what happens with String objects - the String class has an equals method which returns true if both strings contain exactly the same sequence of characters, even if they happen to be two different String objects in memory.

OK...so "==" is not what I thought it was!!!

thank you,JamesCherrill and ~s.o.s~ ^_^

I changed the main class as you guys said, but the result is still "?!!"(according to my prediction, it should be "b1"...)
Here's the edited code...did I just do something wrong again?

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).equals(b1)){   //<-------------this part!
			System.out.print("b1");
		}
		else if(gb.get_c.get(0).equals(b2)){  //<---------this part also!
			System.out.print("b2");
		}
		else{
			System.out.print("?!!");
		}
	}
}

Did you implement "equals" methods?

So...the changes I made is not "implementing equals methods"???
(Do you mean I have to 'create' that method manually? -- no way... T.T)

Yes.

This may help you: for each of yopur classes create a public String toString() method (overrides the defaultg useless implementation in Object) that returns a printable representation of the values in that class, then use it to print out the values before & after serialisation.
Every class should have a toString - it's really valuable for every kind of debugging.

Oh...OTL

anyway, thanks for the details, JamesCherrill

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.