Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently writing a command line app that takes input from a user through the terminal/command line and appends that data into an array list which is then appended to a text file. The user should be able to perform actions like filtering the contents by name, index, etc. For example, if the user wants to print all pets that are "dogs", all dogs and their details should be printed to the terminal. This all works successfully with my program but I've come across a problem.

I'm using getter methods to carry out the filtering, which roughly looks like this.

Java
if (pet.getPet().contains("dogs")) {

// print all dogs.

}


As I understand it, the array list is placed in the garbage collector when the process exits. Because of this, the data is lost after each run, and the getter methods (and ultimately the filtering) doesn't work appropriately on a different run (if that makes sense :) ).

With that said, the data is still appropriately added to the text file and so my attempts have involved trying to read the data from the text file back into the array list but I haven't been successful with this. Whether it involves using text files or some other methods, how might I approach a solution to keep the data in the array list constant, such that I can still use the getter methods as well? Thanks in advance.

On a different note, I would like to post my code, but given that this is a university assessment, I have been advised to be cautious with the code I post. If more detail is required, I'll try to do my best :)

What I have tried:

So far I have tried using a buffered reader to load the contents of the file into a 2d/nested list that takes Strings, by parsing each line of the file into it. This has been my best attempt at the problem since I did successfully parse the file contents into the list, but I've had trouble figuring out how to appropriately append those values to the original array list. I have also tried using serialization, but I came across an error where the data appended to the file did not register on printing the file contents.
Posted
Updated 10-Apr-19 2:43am

It looks your ArrayList needs serialization. See, for instance How to serialize and deserialize ArrayList in Java - HowToDoInJava[^].
 
Share this answer
 
Quote:
How can I permanently store the data of an array list?

Short answer: you don't, at least not directly.
By principle, array list exist only as long as program is running, when program stops, array is killed.
The only way to make its contain permanent is to store the array in a data file and reload the array from file when program is launch again.
Quote:
I would like to post my code, but given that this is a university assessment, I have been advised to be cautious with the code I post.

What do you fear? Do you think Russia will use your code to hack England Nuclear missiles?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900