Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to save data in a Java application?

I'm trying to find the best way to save the state of a simple application. From a DB point-of-view there are 4/5 tables with date fields and relationships off course.

Because the app is simple, and I want the user to have the option of moving the data around (usb pen, dropbox, etc), I wanted to put all data in a single file.

What is the best way/lib to do this?

XML usually is the best format for this (readability & openness), but I haven't found any great lib for this without doing SAX/DOM.

like image 485
fabiopedrosa Avatar asked Feb 04 '09 21:02

fabiopedrosa


Video Answer


2 Answers

If you want to use XML, take a look at XStream for simple serialization of Java objects into XML. Here is "Two minute tutorial".

If you want something simple, standard Java Properties format can be also a way to store/load some small data.

like image 170
Peter Štibraný Avatar answered Sep 19 '22 20:09

Peter Štibraný


consider using plain JAXB annotations that come with the JDK:

@XmlRootElement  
private class Foo {  
    @XmlAttribute  
    private String text = "bar";
}

here's a blog-post of mine that gives more details on this simple usage of JAXB (it also mentiones a more "classy" JAXB-based approach -- in case you need better control over your XML schema, e.g. to guarantee backwards compatibility)

like image 29
Rahel Lüthy Avatar answered Sep 22 '22 20:09

Rahel Lüthy