views:

91

answers:

6

I'm currently developing a desktop application in java, which stores user data such as bookmarks for ftp-servers. When deciding how to save these informations, I ended up using xml, simply because I like the way xpath works. I was thinking about json too, which seems more lightweight.

What is your preferred way to store data in java desktop applications (in general) and why? What about java-persistence, does that have any advantages worth noting? And how much does the size of user data matter? Its not always possible to store data in a database (or preferable), and in my experience xml does not scale well. Let me know what you think!

Nobody has mentioned json yet, which surprises me. No situation where this is suitable?

+5  A: 

Whatever works, gives you good performance and requires the least amount of code.

Java serialization is easy. Should be fast unless you have big object graphs. It might not work if you'd ever want to interoperate with another language or want the file to be human-readable/editabl. It might be brittle.

XML addresses the multi-language interop, can be read and edited by humans. If you use Java, Jaxb is very, very easy and immediate. If you need to store lots of settings, SAX/StAX might be necessary. You might want to use DOM/JDom under very weird circumstances.

If there's lots of settings you want to store and query, an embedded SQL database is an option. I recommend H2.

alex
+1 for JAXB. It is polite to let your users hack the program's files - try doing *that* with Notepad and a serialized object blob :)
gustafc
DOM seems VERY slow in java. Almost to slow to put onto serious use. I cant imagine why though.
Frederik Wordenskjold
@Frederik Wordenskjold: There are many third party implementations, though.
k_b
+5  A: 

Sometimes, simply using properties files is enough.

A properties file is a text file consisting of key/value pairs. The java.util.Properties class is used to interact with property files. However, it's not good for storing lists of things because everything's a key/value pair.

k_b
Seems like the obvious way to store small data-files, like user preferences and such.
Frederik Wordenskjold
Ah! I knew I'd miss something obvious! Upvote! Although for complex structures, .properties files might not be suitable, there's a whole lot of situations where they are pretty handy.
alex
+2  A: 

There's no way to say which I prefer "generally." Even in a single app, I might have a few different ways of serializing user data. Take a computer game, for instance. I want my game preferences to be stored such that I can edit them with a text editor. I want my licensing data stored such that it's globally accessible. I want my highscore data to be stored such that nobody can edit it effectively. I want my saved game state stored such that reads and writes are extremely fast. These four different requirements might lead to four different solutions just within the same app.

Mark Peters
+1  A: 

We use XML if we don't mind user tweaks the data. For more complicated data, we use SQLite. You can find Java implementation here,

http://sqljet.com/

ZZ Coder
+1  A: 

Whatever format is used to build the file, it's a good idea to store the file in the user's home directory:

String userHome = System.getProperty("user.home"); // "/home/myusername"
Michael Angstadt
+2  A: 

There are two aspects:

  1. If you want to implement it quickly with an RDBMS, use an embedded DB like SQLite. This is used by many desktop applications like Firefox.

  2. If performance is a concern, then use go for embedded key-value pairs kind of storage solution.

zengr
+1 for SQLite. That's my preference. Good performance and technical user hackable, plus command line updatable.
blissapp
SQLite in Java?
alex
there are many wrapper out there like sqljet stated by @ZZ Coder above.
zengr
... Now below! ;)
Frederik Wordenskjold