Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Save objects in a textfile? Are there readymade solutions?

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

like image 568
wellenreiter Avatar asked Oct 04 '08 15:10

wellenreiter


People also ask

How do I save an object in Java?

If you have the data in for example a byte array in memory, then yes, you can save that to a file: byte[] data = ...; OutputStream out = new FileOutputStream(new File("C:\\someplace\\filename. dat")); out. write(data); out.

How do I write an object into a text file?

Steps for writing to text files To write to a text file in Python, you follow these steps: First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.

What type of object is used to write output to a file in Java?

FileWriter and BufferedWriter classes are used to write only the text to a file, but the binary data can be written by using the FileOutputStream class. To write data into a file using FileOutputStream class is shown in the following example.


2 Answers

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.

like image 170
Vinko Vrsalovic Avatar answered Sep 21 '22 21:09

Vinko Vrsalovic


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:

  1. How are you going to cope with schema changes (i.e. changing what data your classes consist of - adding a new field for example)
  2. 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
  3. 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!
like image 43
oxbow_lakes Avatar answered Sep 23 '22 21:09

oxbow_lakes