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?

2
  • 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. Commented Mar 31, 2019 at 16:11
  • I decided to go with github.com/cbeust/klaxon thanks. Commented Mar 31, 2019 at 20:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

As mentioned above: I decided to go with github.com/cbeust/klaxon. Thanks anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.