I have an arrayList set up to hold player data. Each player contains several elements, namely jersey number, first name, last name, preferred position, goals, and assists. I want to be able to save this data so that when I restart the program the data is still in the exact same format. From some stuff that I have read it looks like serializing is the way to go? Is this correct or is there a better way? I am new to Java so any examples would be great. I have attached several applicable snippets of my code.
//creation of arrayList
public class LeagueDatabase extends javax.swing.JFrame {
ArrayList <blackTeam> blackTeam = new ArrayList <blackTeam>();
//how class is structured
class blackTeam {
int goals, assists;
String jerseyNum, firstName, lastName, prefPosition;
blackTeam (String _jerseyNum, String _firstName, String _lastName, String _prefPosition, int _goals, int _assists) {
jerseyNum = _jerseyNum;
firstName = _firstName;
lastName = _lastName;
prefPosition = _prefPosition;
goals = _goals;
assists = _assists;
}
}
//how data is added
black = new blackTeam(jerseyNum, firstName, lastName, prefPosition, goals, assists);
blackTeam.add(black);
//what I have so far for saving the data
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try{
FileOutputStream fos= new FileOutputStream("blackTeam");
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(blackTeam);
oos.close();
fos.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
//close program
System.exit(0);
}
});
If someone could explain how to save the data to a file, and then re-load the data from that file when the program is started, that would be great.