1

I have a Kotlin data class

data class Item (val content: String) {}

In my app I use an myData: ArrayList<Item>.

To provide persistant storage the app writes this list to a file everytime it is changed: configFile.writeText(myData.toString())

At startup it reads the file and with configfile.readFile(). The returned string look like this: [Item(content=Click #1), Item(content=Click #2)]

How can I create the arraylist from this string?

liquid.pizza
  • 505
  • 1
  • 10
  • 26
  • 4
    I **very strongly** recommend that you save the data in a structured format, such as JSON. You then use the associated parser (e.g., a JSON parser) for that format to convert the string back into objects. – CommonsWare Mar 31 '19 at 16:11
  • I decided to go with https://github.com/cbeust/klaxon thanks. – liquid.pizza Mar 31 '19 at 20:59

1 Answers1

1

You can write data in some well known format such as JSON or XML. You can still parse your written string, but JSON / XML can be preferrable.

For reading / writing JSON / XML, you can use jackson library which is quite easy to use. Here is the link for quickstart.

Hemant Patel
  • 3,160
  • 1
  • 20
  • 29