Saving the state of a program using serialization - tstorrnetnz/teaching2022 GitHub Wiki

Code listings including whole project as a zip file, main.java and pen.java as separate files are at: https://drive.google.com/open?id=0BzdQz45DMVPtbzBnOE1GSVVCalE


You may find it useful to save the state of a program.

For example, you may have a program where you enter data, names etc and wish to save the program so that the data from a previous execution can be loaded when it is next run. There are several ways to do this. One of them is serialization.

When a Java program is run, the data is stored in RAM. Serialization takes the data in RAM and saves it as a bytestream (binary) file. The bytestream file is not a txt file and can only be read by Java.

Serialization is an easy way to save and reload data. As most of your data will be stored in an arraylist(s) (especially if you are creating an arraylist of objects), the example below shows how to serialize data in an arraylist. It uses a modified version of the pens program (https://repl.it/Gl2A/19). I did this using an editor called BlueJ. Repl would also work. This program does not have a GUI - itโ€™s easier to show this without the extra GUI code. Instead I made a simple text menu for saving.

Recap: The pens program creates an arraylist of pen objects. It has 2 classes a Main Class and a Pen Class. The pen objects are stored in an arraylist called pens.

I used 2 tutorials (both adapted) for this. They are:

https://www.tutorialspoint.com/java/java_serialization.htm

http://beginnersbook.com/2013/12/how-to-serialize-arraylist-in-java/

Overview:

Before using serialization, any classes that are involved need import java.io.*; and implements java.io.Serializable adding to the imports and class definitions

import statement import statement

Do this for both Main and Pen classes.

I adapted the menu from the cars program ( https://repl.it/GLul/43 ) - right at the bottom- so that we had options for adding pens, listing pens, save all and load saved pens. The Main method was changed so that it contains just one line - menu(); so that on starting the menu is called.

menu

The methods for these - you can see them in the if/ else if section are:

if (choice == 1) {addPenMenu();}

else if (choice == 2){listAllPenDetails(pens);}

else if (choice == 3) {saveAll();}

else if (choice == 4) {loadSaved();}

These also need adding to the main method as well as readIntVal and readString from the repl on user inputs (https://repl.it/FlXu/12).

addPenMenu()

addpen

This simply calls readString gets the details and stores them in the variables make, model etc and passes these to the addPen method which stores them in the arraylist pens as pen objects.

listAllPenDetails() This is unchanged from the original programme and simply iterates through the pens array, getting the pen objects and then getting the details from each object.

listallpendetails

saveAll() This is the interesting part where we use serialization.

saveall

loadSaved() This is the reverse process and involves opening the pens.ser file and reading the contents to the pens arraylist.

loadsaved