views:

100

answers:

2

Does the Java provide some sort of registry- or cookie mechanism where I can store small pieces of data to load next start I start an java application- or applet?

For example the application settings such as last opened file etc

+8  A: 

See the Java Preferences API.

It allows you to store preferences per-user and per-system. It'll store preferences in hidden files on Unix/Linux, and use the registry in Windows-based systems (although that's implementation-dependent).

Note: I'm not sure that it'll work with applets (due to security restrictions).

Brian Agnew
The same goes for Mac OS as Unix as suppose?
Viktor Sehr
I would think so
Brian Agnew
+5  A: 

Java Properties are widely used both as persistence format (file) both in memory. They are also widely supported by tools like IDE, ant, maven, etc.

The class Properties is very simple to use and it has several useful method for your purpose (store, load and store):

Properties preferences = new Properties();
preferences.put("color", "red");
preferences.put("style", "bold");
preferences.store(new FileOutputStream("prefs.properties"), "preferences");

// reload the properties
Properties preferences = new Properties();
preferences.load(new FileInputStream("prefs.properties"));

A Java .properties file looks like:

# You are reading the ".properties" entry.
! The exclamation mark can also mark text as comments.
website = http://en.wikipedia.org/
language = English
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
          Wikipedia!
# Add spaces to the key
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
# Unicode
tab : \u0009  
dfa