I have a project that would require that I have an array that frequently has new strings added to it, and in order for the full idea to work, I need it to automatically retain the new strings even after the project ends. I work in netbeans for the most part, so is there a single line of code or some kind of method that I can use to make it so that every newly added object in an array stays in the array after the project ends, as a kind of automatic "save changes"?
Asked
Active
Viewed 338 times
0
-
As in writing the array to a file? – John3136 Mar 11 '15 at 23:19
-
I think so, the idea is that when someone types in a certain response, it runs through a .add(string) method, and I'm trying to figure out if there is a way to make it so that I don't have to manually go in and add that to the array to make it permanent. – PigeonAce Mar 11 '15 at 23:20
-
check [serialization](http://stackoverflow.com/questions/447898/what-is-object-serialization), e.g. native java (non human-readable), see [javadoc](http://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html). alternatively plain text/XML/JSON – harshtuna Mar 11 '15 at 23:44
-
That seems like what I'm looking for, and I think I've heard the term serialization before, but how would I implement it into this code? I'm a little confused on that part. – PigeonAce Mar 11 '15 at 23:50
1 Answers
0
You could create a wrapper for the ArrayList add method in your class which writes to a file when the add method is called and then never directly call the ArrayList
add()
method;
public void add(String value){
// Write to file here - perhaps use the PrintWriter class
list.add(value); // Then add the string to the list
}
Here is a link to the PrintWriter API which would help you get started - http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html
You will need to ensure the file you write to exists otherwise you will get a FileNotFoundException
. A bit of research and you will find out how to do this.
Then you would also need to initialize the list in your constructor from the values held in your file.
Another alternative is to read and write the Strings to a database, for some insight into that look up JDBC.

kstandell
- 775
- 6
- 16
-
So that would have the same effect as having the array be [1,2,3,4] and then by the end having it be [1,2,3,4,5] and having it stay that way without having to physically type in the new object? – PigeonAce Mar 11 '15 at 23:58
-
i'm not quite sure what you're trying to say? physically type in the new object? every time you call the wrapped add method it would write that string to some file you have specified and then add it to the list as well. the file would still remain after... – kstandell Mar 12 '15 at 00:17
-
That's what I meant. The idea is that the user would type in a word classified as a greeting, and if the code doesn't recognize it as a greeting after going through an array containing known greetings (i.e. the array list would be called "greeting" and would contain [Hello, Hi, Hey, What's up, Sup] and if the user typed in "Hola" it would ask if that was a greeting since it doesn't recognize it, and if the user says yes, it would automatically add it to the array "greeting" for later use. If that's what the method you specified would do, what code would you use to add it to the array? – PigeonAce Mar 12 '15 at 00:23
-
The wrapper method I have suggested would do that. As I said, look at the PrintWriter API. Create a new PrintWriter - `PrintWriter writer = new PrintWriter("someFile.txt");` Then you can call the `writer.println(value);` method to write the String to the file you specified - "someFile.txt". There are many ways you can read and write to files, I'm just suggesting one. – kstandell Mar 12 '15 at 10:52