0

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.

4 Answers 4

2

To serialize list all objects that list holds have to be serializable.

So your class blackTeam has to implement Serializable interface.

class blackTeam implements Serializable { ... }
Sign up to request clarification or add additional context in comments.

Comments

0

Yes it seems that serialization is the way to go... For Objects with nested Objects it could be an idea to store the object-values in a java-Database (examples HSQLDB amd H2). These databases are included in the project as jar and as far as I remember store the values in files. The table model then could correspond directly to the Object hierarchy... would make it easier to store and retrieve objects, but may be an overhead for simple objects.

For bigger and nested objects i would suggest to evaluate this alternative as I believe to remember (don't hold me up on this) that serialization has it's limits.

Here two links on googling "java store object as file" link1

link2

Comments

0

Serialization is the path you will need to go down to save your Objects to a file. This here is a good article on how to do so, along with it's examples.

Also, your naming conventions for Class names are incorrect. It should be UpperCamelCased instead of camelCased. However, your variables check out.

Comments

0

You have to decide which way you want to Serialize your data. You can use it with the Java built in Serialization, in which you have to implement the Serializable interface with your blackTeam class.

public class blackTeam implements Serializable {

     //...

}

Now you already have a method to save the file, you just need a method to read the file into an object.

public List<blackTeam> loadTeam() throws IOException, ClassNotFoundException
{
    FileInputStream fileIn = null;
    ObjectInputStream in = null;
    List<blackTeam> teams = null;
    try
    {
        fileIn = new FileInputStream( "blackTeam" );
        in = new ObjectInputStream( fileIn );
        teams = (List<blackTeam>)in.readObject();

    }
    finally
    {
        if( fileIn != null )
            fileIn.close();
        if( in != null )
            in.close();
    }
    return teams;
}

Note, there are other ways to serialize Objects. You could use the JSON notation and save your object in a JSON format to be more transferrable. There are external libraries you can use to do JSON formatting such as Jackson

2 Comments

I implemented serializable but I get the error message "java.io.NotSerializableException: javax.swing.GroupLayout" when the code is run, do you know why this is? As for your example for reading the file into an object; if I am correct your example saves it as a list, not an arrayList like I want it. Also would I call this by doing "loadTeam():" in the code that runs when the program starts? @Orin
That is because the GroupLayout object is not Serializable. So if you have that inside of your object, you cannot serialize it unless you mark it as transient. Yes you need to call loadTeam() when you call the program starting up, which will return the object you want. And yes it saves it as a List, but an ArrayList is an implementation of List. You can interchange my example with ArrayList if you really want to. @ckorvee

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.