Files: reading and writing to files - tstorrnetnz/teaching2022 GitHub Wiki

How to think like a computer scientist (HTTLACS)

Otago Workbook L2

Otago Workbook L3


Learning Aim: Read and write data from and to files so that you can store data for your programs

Any useful program will need to store data. This could be user preferences, the state of a program when closing down/restarting or larger amounts of data e.g. from a spreadsheet or database. Most modern programs, desktop or web, will use a combination of all of these.

In our case we will ignore ‘proper’ databases as they add a layer of complexity that we don’t need. Also we will be making a desktop application. The reason for this is that configuring a web application so that it is secure is also potentially complex. So for our purposes we are looking at a desktop application that can save and read data from either or both of text files (e.g. csv - comma separated files) or ‘binary’ files. A binary file is a file that is not human readable and is often used to store the state of a program before closing.

You’ll notice that we are touching on each technique or strategy quite lightly. That’s because your project in Term 2 can use a range of techniques. It’s important for you to know the range of strategies that can be used - and where to find help for them. It’s less important that you know each strategy in detail yet - if you use it in your project you will have plenty of time to learn it.

Key ideas:

Serialization

I have made a brief (2 minute) overview of what serialization is (without code) that you may find useful. https://youtu.be/fuH1-LVXh7w

Serialization is a pretty nifty technique. In short it allows you to save the program state for use at a later point in time. For example, perhaps your program saves data in an array list. Serialization will save your program is a special file that can be loaded so that the data does not have to be re-entered. I have put together a guide on how to use serialization with an example - https://github.com/tstorrnetnz/teaching2022/wiki/Saving-the-state-of-a-program-using-serialization

An example of this is in a ‘pens with serialization’ repl. https://repl.it/@trevorstorr/Pens-with-menus-and-serialization-1 There are 2 classes - Main and Pen. When you ‘save all’ you should see a file pens.ser appear. This is created in the method saveAll - about line 128. Loading the file takes place in the loadSaved method - about line 146. There is also a text based menu for the program - this is at the end.

Reading/writing to .txt files

What if you had a spreadsheet of data to load? In this case it would be better to load the spreadsheet. We can do this by reading the spreadsheet (where the values are separated by commas (‘) and saved as a csv (comma separated values) or text file. One the data is read we can create an arraylist of pen objects from the data.

I have broken this down in to logical steps. This is covered in detail in the Otago Uni Level 3 workbook - chapter 5 - the examples were taken from there and modified - I suggest you look at these alongside the repls below.

  1. https://repl.it/@trevorstorr/read-from-a-file This reads data from a file - data.txt

  2. https://repl.it/@trevorstorr/read-from-a-file-1 This reads data from a file, puts each line of data (make, model, type, colour, price) as an element in an arraylist and reads the arraylist

  3. https://repl.it/@trevorstorr/read-from-a-file-2 This is the same as #2 but split into separate methods for reading the file and printing the arraylist contents - with a main method to control it all - reads the data into an arraylist

  4. https://repl.it/@trevorstorr/read-from-a-file-3 This build on #3 so that we can access for example the colour or price of a pen separately, rather than all at once - but still as items in an arraylist - not objects of class Pen.

  5. https://repl.it/@trevorstorr/read-from-a-file-4 This is almost exactly the same as #4 - but the data.txt file has a header line so you can be clear what each item is

  6. https://repl.it/@trevorstorr/read-from-a-file-5 This develops the previous examples by reading the data file and making an arraylist of pen objects (using the Pen class from last week)

For you to do:

  • Work through the above examples so that you understand them.

  • Create a fork of the pen repl. Modify it so that pens can be loaded from a txt file. You will need to modify the menu so that it can be controlled - i.e. add an option to load/import from the txt file. If you do this correctly, you should be able to load pens from the data.txt file and add additional pens from the text interface. Can you create an import type functionality that does not overwrite any existing pens in the arraylist?

  • Using the 'pens with serialization repl' example, mentioned above, add both serialization and a menu to the program you created last week (e.g. cars/planes ect).

Extension work

  • Create another example similar to the one you wrote last week, this time for books in a library (if you did books last week think of something else). Have at least 5 fields in the library classes (Title, Author etc….). Add a menu and save data using serialization.

  • Add the ability to load from a txt file to this program you have just created.

  • https://repl.it/@trevorstorr/create-new-file-and-write-to-it Create a new text file and write to it.

  • https://repl.it/@trevorstorr/append-text-to-an-existing-text-file This takes an existing file “names.txt” which has two names in it and adds several more names to the file. There is quite an nice explanation of how this works here: https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java#1625263

  • One interesting question or problem is how to write back to file any changes or edits to an arraylist of objects. Think about this scenario: you load an arraylist of objects e.g. pens, cars etc from a file. You then change the variables in a couple of the objects and add an extra object. When saving, will it be more straightforward to make edits to the txt file, or will it be easier to wipe the old file and create a new version of it using the complete set of data from objects in your arraylist?