Question

How do you store an array list of objects into a file, so that it may be later read back from the file and retain its data?

I am attempting to write a program in Java that uses a GUI to allow the user to input a record, i.e. Contact Info (with a name, an image, contact information), and then store it in a file. Later, they are able to read the entire file as a list of contacts.

I am wondering what the best way to incorporate that into an alphabetically sorted file, so that when read, it will always display the items in the correct order. Currently I save each contact as an individual file, then just load them as they appear in the directory. Is there a way to store them into a single file, then somehow access each record consecutively? I guess what I am really asking is whether it is possible in Java to save an object into a text/data file, then have it read back as an 'object'.

Était-ce utile?

La solution

If you want to stick with serialized objects, why not serialize a whole ArrayList etc.?
If the file isn´t getting big (100MB, 1GB, 10GB...), then there shouldn´t be a problem
with simply store and load everything just like that.

Autres conseils

There's not better way...there are ways... e.g. parse the information into a XML file or use a simplier one like a CSV file Assuming that you sorted the values previously

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top