I have read a lot about serialization and I cannot figure out how I am able to serialize then deserialize multiple objects. I am very much a newb to Java, so please bear with me.
My code currently works, but my logic is flawed. I have an object called "club", which contains several fields to be serialized e.g teamname. I have used case statements with a switch, so that I can serialize more than one object, but I ended up re-trying my try/catch block and streams to get it to work.
Both objects can be independently written to a file, but I am unclear of the syntax to use, to ensure I can implement the following pseudocode.
if teamName = club1 and player would like to save the game serialize club1 then de-serialize club1, if player selected load game.
Finally, could someone point me in the right direction of writing a game "state" to a file, rather than just pre-defined fields that I have declare as constants.
Snippets of my code are as follows :
switch (option) {
case 1:
chooseTeam(clubName);
exit = true;
break;
case 2:
Serialize saveclub1 = new Serialize();
saveclub1.Serialize();
exit = true;
break;
case 3:
DeSerialize load = new DeSerialize();
load.DeSerialize(clubName);
exit = true;
break;
case 4:
credits();
exit = true;
break;
case 5:
exit = true;
System.out.println("Goodbye!");
break;
case 6:
Serialize saveclub2 = new Serialize();
saveclub2.Serialize();
ClubInfo club2 = new ClubInfo();
club2.teamName = "Aston Villa";
club2.stadium = "Villa Park";
club2.division = "Premier League";
club2.SSN = 11122333;
club2.stadiumCapacity = 40000;
try
{
FileOutputStream fileOut =
new FileOutputStream("/home/cg/root/club.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(club2);
out.close();
fileOut.close();
//System.out.printf("Serialized data is saved in C:/tmp/club.ser");
}catch(IOException i)
{
i.printStackTrace();
}
exit = true;
break;
public void DeSerialize(String[] clubName)
{
ClubInfo club = null;
try
{
FileInputStream fileIn = new FileInputStream("/home/cg/root/club.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
club = (ClubInfo) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Club class not found");
c.printStackTrace();
return;
}
public void Serialize()
{
ClubInfo club = new ClubInfo();
club.teamName = "Arsenal";
club.stadium = "Emirates";
club.division = "Premier League";
club.SSN = 11122333;
club.stadiumCapacity = 60000;
try
{
FileOutputStream fileOut =
new FileOutputStream("/home/cg/root/club.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(club);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in C:/tmp/club.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}
public class ClubInfo implements java.io.Serializable{
public String teamName;
public String stadium;
public String division;
public transient int SSN;
public int stadiumCapacity;
} // end ClubInfo class