I’m working with a hand in task that is like a simple bank with customers and different accounts. Now I have to develop my task to store my customer objects and account object to file. I have read some about it and I’m going to use the ObjectOutputStream and the ObjectInputStream.
Since I have all customer objects in an arraylist, called customerList and all account objects that belongs to the customers in an arraylist, called accountsList, I wonder if I should and can save the hole arraylist or should I save all objects separately?
And when I’m going to load the customer object, should I put them in an arraylist again to be able to access them as I have done until know? Looking for a good and simple to understand solution. Preciate the help! Thanks!
Perhaps I can just make a loop and pick each object in the array list and save them as object. And then when I’m going to load them just make a loop again and load them and add them to array list!?
You can save (technical term is serialize) any object as long as they implement
java.io.Serializable.ArrayListimplementsSerializableso as long as your Customer and Account objects implement are also implementingSerializableinterface there will be no problem.Example of serialization with ArrayList: http://www.javabeginner.com/uncategorized/java-serialization
NOTE:
The better way to store long term (and production) data is to use a DBMS (RDBMS or NoSQL DB) system.