Help with object arrays
Hi, I have managed to gather user input using the java.util.Scanner and during runtime this input is stored in the properties of a class Called customer,
how do I set up an array of customer objects
and then save them to a txt file ( this save location will be changed to a database in future. )
basicly I would like to know how to save multiple customers information in a .txt file, by adding the class(object?) to an array and then saving the array to a .txt file.
and how to read it back to be able to print the contents of the txt file to the commandline.
I would use an ArrayList to store Customer objects
//Something like this. This assumes that you followed convention, and 'customer' is actually 'Customer'
Two options to make the data persistent:
ArrayList<Customer> yourVariable = new ArrayList<Customer>();
1. Serialize the object and let Java do the heavy lifting for you.
JDK 5.0 Serialization-related APIs & Developer Guides -- from Sun Microsystems
2. Create a text file with all the data you will need to recreate the objects. When you read the file you will have to recreate the objects with the data you saved in the file. You will have to think of a structure that you will be able to consistently parse when you save the file.
Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)