Implement a Key-Value Store Persister
We persisted Author
objects by writing/reading their properties one after the other. This approach is prone to error because you must make sure your code matches the persisted format for both reading and writing. If you change the Author
class - for example, adding a dateOfBirth
field to it - then you may no longer be able to read from old files because some lines could be read into the wrong properties. A potential solution is to save the data as key-value pairs. Java has the Properties
class, which you can use to store value strings with associated key strings:
Write and run a little demo for this and check out the text file that stores the Author
object.