So I am creating a program which will keep track of the TV shows and Movies that I watch. I have 2 classes (TV and Movie) and I have an Array which holds everyone. How would i go about saving this array so that every time I use the program it lets me edit the same list, because every time I watch a new episode I want to update my array with the new information. So basically what procedure would I need to use in order to not only save an arrya but load up the array every time I run the program?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
While you could serialize your object and write it to disk... I am guessing it would be easier for you to simply output your array to a file and read it back in. You can write each element in the array to the file in a loop fairly easily, and read it back in just as easily. And like the others said, it is worth your time to look into an ArrayList or similar structure! Example:
To write to a file:
To read from a file:
In order to save an array for further reference you need to make the class serializable (for example, by implementing
Serializable
). What this means is that an object can be converted to a sequence of bytes which could be saved to a file or sent over the network, which can later be used to recreate the object in memoryThen you can do this to save the data to a file:
You can read it back like this:
Firstly, you should avoid arrays like the plague... use
Collections
instead, which apart from anything else expand in size automatically as needed (you'll need this).Regardless of whether you use array or a
List
you can serialize the object to disk by writing the object to anObjectOutputStream
wrapping aFileOutputStream
.Note that the objects in the array/List must implement
Serializable
, as must all your class fields.To read the array/List back in, read from an
ObjectInputStream
wrapping aFileInputStream
.You can Google for the countless examples of such code.