0

What I want to do is basically I want to add elements to an arraylist and call those elements back even though the program closes. So I want to store those elements permanently and when I start the program again I want to be able to call the elements I added to the arraylist before and see them. It could be with text files or something else, everything non very complicated is accepted. Thanks!

Selim
  • 45
  • 4
  • This would require you to save array contents to some file or database. Consider creating a config file that has your data, which you read and save to as needed – Frontear Mar 09 '22 at 18:41
  • 1
    You answered your own question. You can write the list to a file. – rhowell Mar 09 '22 at 18:41
  • I dont know how to write it to a file and call it back later. If you could help me with that, it would be great. – Selim Mar 09 '22 at 18:43
  • Here's how you can write it - https://stackoverflow.com/questions/6548157/how-to-write-an-arraylist-of-strings-into-a-text-file here's how to read it - https://stackoverflow.com/questions/5343689/java-reading-a-file-into-an-arraylist a little bit of googling will solve this – rhowell Mar 09 '22 at 18:44
  • Hahaha thank you so much. I've been searching for an answer for 40 minutes however every code I encountered was quite complex so I gave up and asked this question, thanks – Selim Mar 09 '22 at 18:47

1 Answers1

0

Serialize the data into a file and when starting up again deserialize it. Remember to add a shutDownHook in order to save it before the program exits.

Runtime.getRuntime().addShutdownHook(...)

enter link description here

Vml11
  • 361
  • 1
  • 2
  • 11