Hello, I want to save the objects I generated in a program. After restart the App should load automaticly all Objects in an Array. I want to write them in a file and parse them after restart. Are the other smarter possibilities than do it by hand? Thank you
Yes, the concept you are looking for is called serialization. There's a fine tutorial at Sun here.
The idea is that classes you want to persist have to implement the Serializable interface. After that you use java.io.ObjectOutputStream.writeObject() to write the object to a file and java.io.ObjectInputStream.readObject() to read it back.
You can't serialize everything, as there are things that don't make sense to serialize, but you can work around them. Here's a quote about that:
The basic mechanism of Java serialization is simple to use, but there are some more things to know. As mentioned before, only objects marked Serializable can be persisted. The java.lang.Object class does not implement that interface. Therefore, not all the objects in Java can be persisted automatically. The good news is that most of them -- like AWT and Swing GUI components, strings, and arrays -- are serializable.
On the other hand, certain system-level classes such as Thread, OutputStream and its subclasses, and Socket are not serializable. Indeed, it would not make any sense if they were. For example, thread running in my JVM would be using my system's memory. Persisting it and trying to run it in your JVM would make no sense at all. Another important point about java.lang.Object not implementing the Serializable interface is that any class you create that extends only Object (and no other serializable classes) is not serializable unless you implement the interface yourself (as done with the previous example).
That situation presents a problem: what if we have a class that contains an instance of Thread? In that case, can we ever persist objects of that type? The answer is yes, as long as we tell the serialization mechanism our intentions by marking our class's Thread object as transient.
You may want to check out Effective Java for some gotchas with respect to serialization. These are generally advanced but if this feature is a core part of your app, you'll want to know about them in advance.
Examples include security concerns, inheritance, and most importantly the potential to publicly 'freeze' an API with realizing it (e.g. across versions of software). Again, these are all advanced and should not deter you per se.
You can use the Berkeley DB PersistentMap
class to save your (Serializable
) objects in a Map
implementation (a cache) which persists them to a file. It's pretty simple to use and means you don't have to worry about what to save where.
Three things to note about serialization:
- How are you going to cope with schema changes (i.e. changing what data your classes consist of - adding a new field for example)
- What happens if your file(s) become corrupted? Depending on how reliable your program's storage needs to be will affect your decision of how you save the data which your objects implicitly contain. Remember, you can always use a relational database such as MySQL and then convert this data into your objects
- Do you need to be able to view or query the data outside of your program? What if you want to answer some simple question like "How many object's have property X?" Easy to do if you've used a (relational) database; not so easy if you've serialized stuff to files!
java.io.Object[Input/Output]Stream are the two classes you need to look at.
Any class you wish to persist to file needs to implement the java.io.Serializable interface.
You might also want to consider using XML encoding, which seems to me to have more durability than serialization. Use java.beans.XMLEncoder/XMLDecoder
classes.
I concur with mjlee that Protocol Buffers from Google is a better way to do store objects than the original serialization way. Take a look and you will love it.