Search code examples
javaserializationstore

Is serializing in Java the best/easiest way to store and later access (a small amount of) data?


I am relatively new to Java and have much more experience with Matlab. I was wondering what the best way is to store a relatively small amount of data, which has been calculated in one program, that should be used in another program.

Example: program A computes 100 values to be stored in an array. Now I would like to access this array in program B, as it needs these values. Of course, I could just write one program all together, which also implements the part of A. However, now every time I want to execute the total program, all the values have to be calculated again (in part A), which is a waste of resources. In Matlab, I was able to easily save the array in a .mat file and load it in a different script.

Looking around to find my answer I found the option of serializing (What is object serialization? ), which I think would be a suitable for doing what I want. My question: is serializing the easiest and quickest solution to store a small amount of data in Java, or is there a quicker, more user-friendly option (like .mat files in Matlab)?


Solution

  • I think you have several options to do this job. Java object serialization is one possible way. From my point of view there are other options to serialize the data:

    • Write and read a simple text file to store the computed values.
    • Using Java Architecture for XML Binding (JAXB) to write annotated Java classes to XML file. Same for JSON is also available.
    • Using a lightweight database like SQLite or HSQLDB (native Java database).
    • Using Apache Thrift or Protocol Buffer to de/serializing Java objects to files.