0

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)?

Community
  • 1
  • 1
  • If you want to access it _later_, then i assume you will have to store it on disk. And anything stored on disk is serialized there is no other way. – litelite Jan 18 '17 at 14:27
  • Instead of object serialization you could write out plain text or csv or something. A plain text file would be closest to .mat, I think. – Fildor Jan 18 '17 at 14:27
  • 1
    Serializing ties you to a specific class implementation. As you change the code and your class evolves, its serialized form will change. – RealSkeptic Jan 18 '17 at 14:27
  • @litelite I do not want to split hair over this. I referred to "default" object serialization if that's clearer. – Fildor Jan 18 '17 at 14:29
  • 1
    @litelite My answer holds for any form of serialization. As the object changes, the serialized form will change. It's usually better to save the relevant data in some format, rather than tie it to the object implementation. – RealSkeptic Jan 18 '17 at 14:29
  • @RealSkeptic unless he takes the builtin java serializer, he is not nescesserally tied to the implementation – litelite Jan 18 '17 at 14:32
  • 1
    @litelite you seem to be mixing serialization with persistence. Not every data write in the world is serialization, and you won't find your definition in the very answer in the link in the OP, because that is talking about object serialization. – RealSkeptic Jan 18 '17 at 15:00

1 Answers1

2

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.
Community
  • 1
  • 1
entenfuss
  • 63
  • 3
  • These are all serialization. Just not with the built-in serializer. – litelite Jan 18 '17 at 14:35
  • 1
    @litelite you are right, but this is not helpful. We can agree on that these are different forms of serialization of which some are better suited than others for the OP's case, can we? – Fildor Jan 18 '17 at 14:40
  • @Fildor yes, off course, I just wanted to point out that the way the answer is written it seems as if the other options are not serialization when thay actually are. – litelite Jan 18 '17 at 14:43
  • 2
    @litelite I hope you will agree now to my updated text :-) – entenfuss Jan 18 '17 at 14:49