Please enable JavaScript to use CodeHS

Data Persistence in Java

In this tutorial, you will have a chance to explore data persistence using Java file input and output.

By David Burnham

Many of the programs that you create allow for user input and produce an output, but each time you run them, the data resets. Keeping data from one run to another is known as data persistence. Data persistence is very common for software developers. Image if you had to enter your contacts every time you opened up your app on your phone. It would not be very effective.

Data persistence can be accomplished in many different ways and often depends on the system that you are using. In this example, you are going to explore reading and writing to a file to save data between runs. This tutorial will build off the File I/O in Java tutorial, but will not cover the basics of file I/O.

The basic flow for data persistence is to read a data file in and use it to populate your initial data. From there, the program can run with the data. Anytime data is added (or you choose to manually save data), data is written back out to the file.

Check out the basic example below. Notice that the shopping list is read into an ArrayList, then it prompts to add an item and then saves the list back out. Each time you run the program, it will remember the items from the previous execution.

One thing to note, because of the way that the CodeHS servers implement this example, your data will be erased after about a minute of inactivity. If you see this message: Server disconnected. Press Run to reconnect., then your data has been reset. When implementing this on your own, you will not have this issue.

In the example above, you should notice that each line of data was one element. Often times your data may be more complex and will require more processing. Take a look at the example below where you are using the Student class to save the student’s name and test scores.

Notice in this example that the basic flow is the same, but when you read and write your data, you are using commas to separate data elements. It is important to understand how the data is written so that it can be read back in correctly.

When reading the data in, you first read the line, then split the line into a string array. From the array, you create the Student object and load the data.

The above example requires a little bit more work on the end user’s part. Since the Student object contains all of the data, an alternate approach can be to create a behavior in the Student class to handle the input and output.

Check out the example below and notice that the GradeBook class now passes the raw data feed to the Student class and receives the complete output string from the Student class in order to write the data. In the Student class, it is easier to process the data since the data elements are members of the class. There is also a new constructor in the Student class to accommodate creating a student without specifying a name.